![](/img/trans.png)
[英]Run test task with library but package does not exist in gradle with 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.