2017-10-12 127 views
2

Gradle不會使用@Category和@RunWith註釋運行我的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註釋,搖籃運行測試。 scanForTestClasses = false設置不起作用。

+0

號它是搖籃。 – isobretatel

+0

它看起來像PowerMock用代理替換@Category註釋:https://stackoverflow.com/questions/13377634/runwithpowermockrunner-class-does-not-work-with-package-annotation?rq=1 – isobretatel

回答

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); 
    } 
} 
+1

感謝你,只是遇到了同樣的問題。值得注意的是,「FastTest」也必須是完全合格的軟件包/類名,就像「Category」完全合格。 – doeiqts

相關問題