最近在使用Maven管理项目,开始测试的时候用到单元测试方法(@Test)JUnit.
我的测试代码:
@Test
public static void testNamedQuery() {
...
}
但是在加上@Test之后选择JUnit运行时就报错
如下:initializationerror Runner JUnit4
经过查找资料,大神都说是缺少了一个jar包,因为这三个jar要一起使用(大牛说的)
junit-4.12.jar
hamcrest-library-1.3.rc2.jar
hamcrest-core-1.3.rc2.jar
于是我对比我的jar包发现,确实少了一个hamcrest-library-1.3.rc2.jar包,
然后就在我的pom.xml文件中加入hamcrest-library依赖,让maven替我配置jar包
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
</dependency>
然后我的jar包就全了.
于是我再JUnit运行,还是报错!!!
愤 怒 !!!
突然想到再查资料的时候看到一位仁兄说他是因为方法上没有加public,
我突然想到是不是我加了static,(因为之前是写main方法调用的,用静态的方法方便).于是我把static去掉之后再运行.
@Test
public void testNamedQuery() {
...
}
果然成功了!!!
ps:第一次感觉绿色也这么好看.