使用 maven 运行时跳过春季测试



我正在尝试在Maven构建或安装的春季项目上运行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.发布

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

你从某个父级那里继承了这个POM吗?因为surefire-plugin的这种配置可能位于父级中,从而导致跳过测试。

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

因此,您需要从父项目中删除 skipTests 属性,或者显式允许子项目中的测试。

最新更新