我们可以将 spring boot 1.4.1 测试框架与 java 1.7 一起使用吗?

Can we use spring boot 1.4.1 testing framework with java 1.7?

我在 java 1.7 上使用 spring boot 1.4.1。我有一个带有一个获取端点的示例控制器 "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 进行测试吗?

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):

Use Jetty 9.2 with Maven

If you are using the starters and parent you can just add the Jetty starter and override the jetty.version property:

<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>