简体   繁体   中英

SpringJunit4ClassRunner — can I change the lifetime of the injected resources?

By experiment, I find that SpringJunit4ClassRunner treats the context and its beans as 'class scope' in the JUnit sense of scope. It inititializes my beans once for the entire set of tests in the class.

Is there any way to use this mechanism and get these things to be 'test scope'? In short, I wish that the context was being loaded as @Before instead of @BeforeClass .

You can annotate test methods that dirty the Spring context with @DirtiesContext , (documented here ) causing the context to be re-loaded for subsequent tests in the class.

I realise that this isn't quite what you're asking for, but perhaps it will do what you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM