在运行junit测试的时候,发生一下异常:
- java.lang.UnsupportedClassVersionError: javax/servlet/ServletContext : Unsupported major.minor version 51.0
- at java.lang.ClassLoader.defineClass1(Native Method)
- at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
- at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
- at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
- at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
- at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
- at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
- at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
- at java.lang.Class.getDeclaredConstructors0(Native Method)
- at java.lang.Class.privateGetDeclaredConstructors(Class.java:2398)
- at java.lang.Class.getConstructor0(Class.java:2708)
- at java.lang.Class.getDeclaredConstructor(Class.java:1987)
- at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
- at org.springframework.test.context.TestContextManager.retrieveTestExecutionListeners(TestContextManager.java:232)
- at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:120)
- at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:109)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
- at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
- at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
- at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
- 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 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
- at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
- at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.1.0</version>
- <scope>provided</scope>
- </dependency>
由于这个jar包是用jdk1.7编译的,我的工程是用jdk1.6,于是发生了以上错误。
数据源的class文件编译的jdk的版本比我的服务器上的jdk的版本要高造成的,比如class文件是从jdk1.7编译出来的class,放到jdk1.6的tomcat服务器上就会报这个错
ps:之前没写单元测试,开发环境都是部署在tomcat下测试的,由于这个jar包的scope是provider,意味着servlet-api不会部署到tomcat低下(打war包也不会打进去),而是用tomcat自身的servlet-api,所以没有发现以上错误。
解决方案:
把servlet-api的版本换成用jdk1.6编译的版本就可以了,我用的是
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>javax.servlet-api</artifactId>
- <version>3.0.1</version>
- <scope>provided</scope>
- </dependency>