Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run code before SpringJUnit4ClassRunner context initialization?

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.

like image 251
membersound Avatar asked Mar 26 '15 09:03

membersound


1 Answers

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

like image 157
Evgeniy Dorofeev Avatar answered Nov 15 '22 18:11

Evgeniy Dorofeev