【问题标题】:Spring tests skipped when run with maven使用 maven 运行时跳过 Spring 测试
【发布时间】:2013-09-15 10:56:12
【问题描述】:

我正在尝试在 Maven 构建或安装的 Spring 项目上运行 JUnit 测试。

src/main/test中的测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
        "classpath:mvc-dispatcher-servlet.xml", 
        "classpath:appContext.xml",
        "classpath:databaseContext.xml"})
    public class Test {
        @Autowired
        private EventService evtService;

        @org.junit.Test
        public void test1(){
            List<String> eventNames = evtService.getEventNames();
            Assert.assertTrue(eventNames.size() > 0);
        }
    }

在 POM 中:

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.7.2</version>
    <configuration>
        <parallel>classes</parallel>
        <threadCount>10</threadCount>
    </configuration>
</plugin>

当我使用 Run as -> JUnit 测试从 eclipse 运行测试类时,它工作正常,但是当我运行 Maven Install 时,我得到:

 T E S T S
-------------------------------------------------------
Concurrency config is parallel='classes', perCoreThreadCount=true, threadCount=10, useUnlimitedThreads=false
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

版本:JUnit:4.11;春季:3.2.3.RELEASE

【问题讨论】:

  • 哪个版本的 Junit 在你的 pom.xml 中被声明为依赖项?

标签: spring unit-testing maven testing junit


【解决方案1】:

如果您在问题中提到的 The test class in src/main/test 是正确的,那么这就是一个简单的问题,因为测试必须位于 /src/test/java 中。

【讨论】:

    【解决方案2】:

    您是否从某个父级继承此 POM?因为 surefire-plugin 的此配置可能在您的父级中,导致您的测试被跳过。

    <configuration>
        <skipTests>true</skipTests>
    </configuration>
    

    因此,您需要从父项目中删除 skipTests 属性或明确允许在子项目中进行测试。

    【讨论】:

    • POM 没有父级。我还尝试了“mvn install -Dmaven.test.skip=false”,但仍然得到“没有要运行的测试。”
    • 另外添加 'false' 到 surefire conf 也没有用。
    • 试试@khmarbaise 在这个答案中提到的内容
    猜你喜欢
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 2021-04-30
    • 2020-01-07
    • 2011-07-21
    • 1970-01-01
    相关资源
    最近更新 更多