I'm working on an application that needs to run up a TCP server as one of the first things it does. This is currently initiated via a Spring config class:
@PostConstruct
public void initTCPServer() {
// Gets the port number and other values from the database...
// Note: This uses dependency injection of the Data Access Objects (DAOs).
}
It works fine when using an existing pre-populated database but I'm running into problems when trying to write an integration test: The data needs to be pre-populated but the @PostConstruct
is firing before the data population if it is in a JUnit @Before
method and similarly when using SpringJUnit4ClassRunner's @TestExecutionListeners
.
The only solution I can think of now is to initialise data in a test config class with a @PostConstruct
and ensure this is loaded first - but this seems a bit dirty. EDIT: Just tried and this failed with a BeanCurrentlyInCreationException - looks like dependency injection of the EntityManagerFactory hadn't finished.
Is there a more elegant way (e.g. should I be running up the TCP server somewhere else, i.e. not managed by Spring)?