引入
在做项目当中我们经常会进行单元测试,从最开始傻傻的使用Application测试到后来做基于jsp+servlet的web项目用junit4测试,直到现在在做SSM项目的也常常使用JUnit4,下面就记录我在Spring中使用Junit4所遇到的一个小异常。
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml",
"classpath:applicationContext-mysql.xml"})
public class TestLoveBokeMapper {
@Autowired
XxxxMapper mapper;
@Before
public void Before(){}
@Test
public void testQueyLoveBokeAll(){
List<XXXX> XXXX= mapperM.XXXXXX();
// XXXX.forEach((XXXX XXXX)-> System.out.println(XXXX.toString()));
}
}
异常代码
java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<clinit>(SpringJUnit4ClassRunner.java:102)
... 14 more
解决思路
出现 java.lang.ExceptionInInitializerError
异常由于类加载过程中静态块初始化过程失败导致的,当然这对我们代码没有太大的用处,继续往下看异常代码,我们发现了一个奇怪的东西
Caused by: java.lang.IllegalStateException: SpringJUnit4ClassRunner requires JUnit 4.12 or higher.
这句话意思就是说**SpringJUnit4ClassRunner要求JUnit 4.12或更高。**所以我们找到了错误的原因,更新下自己导入的JUnit吧…