简体   繁体   English

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

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

Gradle does not run my JUnit test with @Category and @RunWith annotations. Gradle没有使用@Category和@RunWith注释运行我的JUnit测试。

Java 8, Gradle 4.2.1. Java 8,Gradle 4.2.1。

My JUnit class: 我的JUnit类:

public interface FastTest {
}

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

My build.gradle: 我的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'  }
}

If I remove RunWith annotation, Gradle runs the test. 如果我删除RunWith注释,Gradle将运行测试。 The scanForTestClasses = false setting has no effect. scanForTestClasses = false设置无效。

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

PowerMock replaces Category annotation with a proxy: RunWith(PowerMockRunner.class) does not work with package annotation PowerMock用代理替换Category注释: RunWith(PowerMockRunner.class)不适用于包注释

Workaround: Add PowerMockIgnore annotation to JUnit 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.

相关问题 使用库运行测试任务但是使用Junit在gradle中不存在包 - Run test task with library but package does not exist in gradle with Junit gradle测试任务如何运行特定的junit测试套件? - how can gradle test task run specific junit tests' suite? 如何在 Gradle 中运行 JUnit 5 和 JUnit 4 测试套件? - How to run JUnit 5 and JUnit 4 test suites in Gradle? Junit测试在Eclipse中运行但是没有使用`gradle test` - Junit test run in Eclipse but fails with `gradle test` 如何通过gradle在特定文件夹中运行jUnit测试? - How to run jUnit test in specific folder by gradle? 使用gradle使用外部参数运行JUnit测试 - Run JUnit test with external parameter using gradle 如何在每个JUnit @Test方法之前单独运行一些代码,而不使用@RunWith和AOP? - How to run some code before each JUnit @Test method individually, without using @RunWith nor AOP? Spock测试与junit 5测试一起无法运行 - Spock test together with junit 5 test does not run IntelliJ JUnit 5测试运行正常作为Gradle任务。 当作为单独测试运行时,有时会出现异常:NoClassDefFoundError ... / TestExecutionListener - IntelliJ JUnit 5 test run fine as Gradle task. When run as individual test sometimes get exception: NoClassDefFoundError …/TestExecutionListener 在RunWith批注中使用SerenityParameterizedRunner进行的junit测试:未找到测试(java.lang.Exception:未找到与Method匹配的测试) - junit Test with SerenityParameterizedRunner in RunWith annotation: does not find tests (java.lang.Exception: No tests found matching Method)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM