Gradle测试任务不使用@Category和@RunWith注释运行JUnit测试

时间:2017-10-12 21:49:57

标签: java gradle junit

Gradle没有使用@Category和@RunWith注释运行我的JUnit测试。

Java 8,Gradle 4.2.1。

我的JUnit课程:

public interface FastTest {
}

@Category(FastTest.class)
@RunWith(PowerMockRunner.class)
public class MyTest {
    @Test
    public void testMyMethod() {
        // Test method should fail
        assertTrue(false);
    }
}

我的build.gradle:

apply plugin: 'java'

repositories { mavenCentral() }

dependencies {
    compile "junit:junit:4.12"
    compile "org.powermock:powermock-core:1.6.5"
    compile "org.powermock:powermock-api-mockito-common:1.6.5"
    compile "org.powermock:powermock-module-junit4:1.6.5"
    compile "org.powermock:powermock-api-mockito:1.6.5"
}

test {
    scanForTestClasses = false

    useJUnit { includeCategories 'FastTest'  }
}

如果我删除RunWith注释,Gradle会运行测试。 scanForTestClasses = false设置无效。

1 个答案:

答案 0 :(得分:3)

报告的Gradle问题:https://github.com/gradle/gradle/issues/3189

PowerMock使用代理替换类别注释:RunWith(PowerMockRunner.class) does not work with package annotation

解决方法:将PowerMockIgnore注释添加到JUnit类:

@PowerMockIgnore({ "org.junit.experimental.categories.Category", "mypackage.FastTest" })
@Category(FastTest.class)
@RunWith(PowerMockRunner.class)
public class MyTest {
    @Test
    public void testMyMethod() {
        // Test method should fail
        assertTrue(false);
    }
}