Reload or refresh a Spring application context inside a test method?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 08:30:27

By design, programmatic refreshing of an ApplicationContext is not explicitly supported by the Spring TestContext Framework. Furthermore, it is not intended that a test method refresh a context.

Thus I would recommend that you reassess your need for a refresh and consider alternatives like placing test methods that require a different set of active profiles in a dedicated test class.

In summary, @ActiveProfiles supports declarative configuration (via value and profiles attributes) and programmatic configuration (via the resolver attribute) of the active profiles for tests, but only at the test class level (not at the method level). Another option is to implement an ApplicationContextInitializer and configure that via @ContextConfiguration(initializers=...).

The only other way to affect the ApplicationContext before it is refreshed is to implement a SmartContextLoader or extend one of the provided classes and configure it via @ContextConfiguration(loader=...). For example, AbstractGenericContextLoader.customizeContext() allows one to "customize the GenericApplicationContext created by the loader after bean definitions have been loaded into the context but before the context is refreshed."

Best regards,

Sam (author of the Spring TestContext Framework)

Not all application contextes support multiple refresh. According to javadoc for AbstractRefreshableApplicationContext only subclasses of it or of AbstractRefreshableWebApplicationContext accept refresh more than once ... and GenericApplicationContext in not one of them.

You should use another class for your ApplicationContext to support hot refresh.

Edit :

As you are using @ContextConfiguration annotation, you should use a custom ContextLoader or SmartContextLoader implementation to force spring to use a less stupid ApplicationContext. But I never found a clean and neat way to to that. So when I need a XmlWebApplicationContext in my test classes, I do not use @ContextConfiguration but create and refresh my context by hand in a @Before method or at the beginning of a test.

I recognize this does not really answers your question, but you can see it as a workaround.

There's a nice little hack to trigger a context refresh - to use org.springframework.cloud.context.refresh.ContextRefresher.

I'm not 100% sure this method will suite you: it requires a spring-cloud-context dependency. However, this may be added just as a test dependency and not leak into production classpath.

To use this refresher you also need to import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration configuration, which adds a RefreshScope scope to your applicationContext which is actually doing the job under the hood.

So, modify test as follows:

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.context.refresh.ContextRefresher;    
// your other imports


@WebAppConfiguration
@ContextConfiguration(locations = {"/web/WEB-INF/spring.xml"}, classes = RefreshAutoConfiguration.class)
@ActiveProfiles(resolver = BaseActiveProfilesResolverTest.class)
public class ControllerTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private ContextRefresher contextRefresher;

    @Test
    public void test() throws Exception {
        // doSmth before
        contextRefresher.refresh();
        // context is refreshed - continue testing
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!