RunWith批注运行JUnit测试

RunWith批注运行JUnit测试

本文介绍了Gradle测试任务无法使用@Category和@RunWith批注运行JUnit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gradle不能使用@Category和@RunWith批注运行我的JUnit测试.

Gradle does not run my JUnit test with @Category and @RunWith annotations.

Java 8,Gradle 4.2.1

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:

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

If I remove RunWith annotation, Gradle runs the test.The scanForTestClasses = false setting has no effect.

推荐答案

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

Reported Gradle issue: https://github.com/gradle/gradle/issues/3189.

PowerMock用代理替换类别注释: RunWith(PowerMockRunner.class)不适用于程序包注释

PowerMock replaces Category annotation with a proxy: RunWith(PowerMockRunner.class) does not work with package annotation

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

Workaround: Add PowerMockIgnore annotation to JUnit class:

@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);
    }
}

这篇关于Gradle测试任务无法使用@Category和@RunWith批注运行JUnit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:04