簡體   English   中英

在JUnit中使用@RunWith有解決方法嗎?

[英]Is there a workaround for using @RunWith in JUnit?

我有一個主要測試類(A)和另一個(B)從它延伸。

A有一個@RunWith(SpringJUnit4ClassRunner.class)注釋來初始化它。 我需要在B上使用@RunWith(JUnitParamsRunner.class)注釋。

但似乎我無法做到這兩點。 有沒有一種方法可以在沒有@RunWith注釋的情況下參數化測試套件?

另一種解決方案是使用

testContextManager = new TestContextManager(AbstractInvoiceCreationModuleTest.class);
testContextManager.prepareTestInstance(this);

但我不能在@BeforeClass方法中寫這個,因為它是靜態的,不允許使用它。 我不希望在@Before方法中編寫上面的代碼,因為類會在每個測試方法之前被初始化。 而且很多測試類都來自A.

可以做些什么?

Spring在版本4.2中添加了對基於@Rule的JUnit支持Spring Context的支持。

http://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/integration-testing.html#testcontext-junit4-rules

該文檔的示例:

// Optionally specify a non-Spring Runner via @RunWith(...)
@ContextConfiguration
public class IntegrationTest {

   @ClassRule
   public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

   @Rule
   public final SpringMethodRule springMethodRule = new SpringMethodRule();

   @Test
   public void testMethod() {
      // execute test logic...
   }
}

您可以使用規則而不是SpringJUnit4ClassRunner.classhttp//www.alexecollins.com/tutorial-junit-rule/

我不知道具體的情況(春季測試跑步者),但我有一個類似的情況,最終創建了自己的跑步者。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM