繁体   English   中英

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

[英]Gradle test task does not run JUnit test with @Category and @RunWith annotations

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设置无效。

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

PowerMock用代理替换Category注释: RunWith(PowerMockRunner.class)不适用于包注释

解决方法:将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);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM