我在java 1.7上使用spring boot 1.4.1。我有一个带有一个get端点的样本控制器" show"
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = SomeController.class)
public class SomeControllerTest {
@Autowired
MockMvc mockMvc;
@Test
public void showPage() throws Exception {
mockMvc
.perform(get("/show"))
.andExpect(content().string(containsString("Some Result")));
}
}
以下是我尝试执行测试时遇到的错误。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mockMvcWebClientBuilder' defined in class path resource [org/springframework/boot/test/autoconfigure/web/servlet/MockMvcWebClientAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder]: Factory method 'mockMvcWebClientBuilder' threw exception; nested exception is java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=org/eclipse/jetty/websocket/api/Session, offset=6
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1023) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.3.RELEASE.jar:4.3.3.RELEASE]
仔细看看原因是
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.test.web.servlet.htmlunit.MockMvcWebClientBuilder]: Factory method 'mockMvcWebClientBuilder' threw exception; nested exception is java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=org/eclipse/jetty/websocket/api/Session, offset=6
有人可以告诉我是否可以使用java 1.7进行测试?
答案 0 :(得分:1)
Spring Boot 1.4.1包含Jetty 9.3(see spring-boot-dependencies pom.xml)
Their documentation说Jetty 9.3需要Java 8+。您应该通过重新定义pom中的属性来替换Jetty 9.2:
<properties>
<jetty.version>9.2.19.v20160908</jetty.version>
</properties>
这恰好是Spring的确切推荐(in their documentation):
将Jetty 9.2与Maven一起使用
如果您使用的是初学者和家长,您可以添加Jetty 启动并覆盖jetty.version属性:
<properties> <jetty.version>9.2.17.v20160517</jetty.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> </dependencies>