In my application I initialize a property before spring application startup as follows:
MapLookup.setMainArguments(new String[] {"logging.profile", profile}); //from args
SpringApplication.run(source, args);
(just for reference: it is used for log4j2
logging, which must be set before spring starts to initialize).
Now I want to run an @IntegrationTest
, but use the same logging configuration. Obviously I cannot use the code above, as a JUnit
test is not executed using SpringApplication.run
.
So, how could I initialize code before a @RunWith(SpringJUnit4ClassRunner.class)
starts?
Note: BeforeClass
does not work as this is executed after spring context startup.
You can run the initialization in a static initializer. Static initializer will run after JUnit loads the test class and before JUnit reads any annotations on it.
Alternatively you can extend SpringJUnit4ClassRunner with your own Runner initialize in it first and then run SpringJUnit4ClassRunner
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With