[英]Using JUnit RunListener in IntelliJ IDEA
我正在開發項目,我需要在運行每個JUnit測試之前執行一些操作。 使用可以添加到JUnit核心的RunListener
解決了此問題。 項目程序集是使用Maven完成的,所以我在我的pom
文件中有這些行:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<properties>
<property>
<name>listener</name>
<value>cc.redberry.core.GlobalRunListener</value>
</property>
</properties>
</configuration>
</plugin>
所以,一切都有效:
mvn clean test
但是當使用IntelliJ(使用其內部測試運行器)開始測試時,我們的RunListener
中編碼的操作不會被執行,因此無法使用IntelliJ基礎結構執行測試。
正如我所看到的,IntelliJ沒有從pom文件解析這個配置,所以有沒有辦法明確告訴IntelliJ將RunListener
添加到JUnit核心? 可能在配置中使用了一些VM選項?
使用漂亮的IntelliJ測試環境而不是讀取maven輸出要方便得多。
PS我需要執行的操作基本上是靜態環境的重置(我的類中的一些靜態字段)。
我沒有看到在Intellij中指定RunListener的方法,但另一種解決方案是編寫自己的客戶Runner並在測試中注釋@RunWith()。
public class MyRunner extends BlockJUnit4ClassRunner {
public MyRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
// run your code here. example:
Runner.value = true;
super.runChild(method, notifier);
}
}
示例靜態變量:
public class Runner {
public static boolean value = false;
}
然后像這樣運行你的測試:
@RunWith(MyRunner.class)
public class MyRunnerTest {
@Test
public void testRunChild() {
Assert.assertTrue(Runner.value);
}
}
這將允許您在沒有RunListener的情況下進行靜態初始化。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.