2016-10-04 37 views
1

我正在使用我在網上找到的這個API,它恰好適合我的目的,但它使用它自己的跑步者,並且不允許我使用Spring自己的,這會導致過多的問題。如何在該文件中加載上下文?如何爲SpringJUnit4ClassRunner.class以外的類加載@ContextConfiguration?

我想加載一些與DAO btw相關的bean,是否有可能沒有@ContextConfiguration?

+1

你可以發佈您的代碼,它不工作,否則你就不會這麼有用的答案。無論如何,你可以在你的Before Tests方法中手動連接上下文,例如[here](https://www.mkyong.com/spring3/spring-3-hello-world-example/) – AntJavaDev

+1

花點時間和[閱讀參考指南](http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-junit4-rules)。 –

+0

請參閱http://stackoverflow.com/questions/24431427/multiple-runwith-statements-in-junit瞭解一些想法。特別是,看看這個API的運行者實際上做了什麼 - 您可以通過調用@BeforeClass中的方法來完成所有工作;讓你自由地使用Spring的亞軍。或者看看JUnit5,我聽說它採用了不同的方法;有效地允許多個跑步者。 –

回答

1

正如在評論中所建議的:而不是使用Spring JUnit亞軍,你可以use JUnit 4 Rules with the @Rule/@ClassRule annotations。 笨拙,你必須同時使用類級別和方法級別的規則,以獲得完整的功能:

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

@Rule 
public final SpringMethodRule springMethodRule = new SpringMethodRule(); 
相關問題