【问题标题】:Simplest spring-mvc testing setup fails to start最简单的 spring-mvc 测试设置无法启动
【发布时间】:2016-09-02 12:41:28
【问题描述】:

我在 Maven 中使用基于 Java 的配置,我已最小化设置以隔离错误,但我不知道是什么原因导致它除了删除 @EnableWebMvc 注释(我显然需要)允许测试开始并通过。

ExampleTest.java

package co.farel.server.services.test;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import co.farel.server.services.test.config.WebConfig;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={WebConfig.class})
public class ExampleTest {

    @Test
    public void test() {
        assertEquals(true, true);
    }

}

WebConfig.java

package co.farel.server.services.test.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mikolaj.nalecz.testy</groupId>
    <artifactId>junit-spring-hibernate</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
    </dependencies>

</project>

当我尝试运行测试时,我得到:

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to process import candidates for configuration class [co.farel.server.services.test.config.WebConfig]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport

整个堆栈跟踪http://pastebin.com/0DxXAS0P

【问题讨论】:

    标签: java spring maven spring-mvc junit


    【解决方案1】:

    您收到此错误是因为您的项目中有无效的import 语句。

    无效的import 很可能如下:

    import org.springframework.context.annotation.Configuration;
    

    尝试将以下依赖项添加到您的 POM:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.5.RELEASE</version>
    </dependency>
    

    【讨论】:

    • 在我的测试环境的完整版中,我确实有这些导入,但问题仍然存在。
    【解决方案2】:

    请在您的测试类中添加注释 @WebAppConfiguration

    希望对你有帮助

    谢谢

    【讨论】:

    • 可惜我之前试过,不影响结果。
    【解决方案3】:

    问题可能是由于容器将在运行时提供 Java servlet 类,但是在测试期间我需要一个 jar 来提供它们。

    添加后:

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    

    到 pom.xml 和:

    @WebAppConfiguration
    

    对于我的 ExampleTest.java,一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-17
      • 2019-01-26
      • 2020-07-13
      相关资源
      最近更新 更多