1.首先添加junit的jar包,创建测试类。
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:spring/ApplicationContext.xml"})
public class Test {
@Resource
NewAppStartService newAppStartService;
@org.junit.Test
public void test() {
try {
PageData pd = new PageData();
List<PageData> varList = newAppStartService.list(pd);
System.out.println(varList);
} catch (Exception e) {
e.printStackTrace();
}
}
}
locations的值可以去 web.xml中寻找,如果路径不对,会报错提示。
过程中遇到了 以下错误:
java.lang.NoSuchMethodError: org.springframework.core.CollectionFactory.createLinkedMapIfPossible(I)Ljava/util/Map;
通过调查得到 spring-mock 和 junit冲突,去掉spring-mock就可以了。