2

suppose you have the following situation while testing a spring context

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConfigClass.class})
public class IntegrationTest   {

 @ClassRule 
 static PluginThatSetsUpSomethingEmbedded pluginRule = new PluginThatSetsUpSomethingEmbedded();



}

now one of my Spring beans needs something that can only come out of the rule, like:

pluginRule.getEmbeddedToolConfig()

how can I provide my bean with that config, notice that the rule is ClassRule and is static too...

gotch4
  • 13,093
  • 29
  • 107
  • 170

1 Answers1

1

I suggest you to use ApplicationContextInitializer and your test class will be something like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConfigClass.class}, initializers = IntegrationTest.Initializer.class)
public class IntegrationTest   {

 @ClassRule 
 static PluginThatSetsUpSomethingEmbedded pluginRule = new PluginThatSetsUpSomethingEmbedded();

 public static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
    }


}

And in method initialize you can implement your logic.

yanefedor
  • 2,132
  • 1
  • 21
  • 37