Ray
Ray

Reputation: 1828

Spring Boot integration test with @PostConstruct depends on @Before section

I write integration test for SpringBoot application with opportunity to run multiple tests simultaneously they have file system dependencies that's why I need to create unique root folder for each integration test.

I have a spring bean in production app that have @PostConstruct section to perform long-running operations. These long-running operations rely on file system structure that I prepare in @Before section of unit-tests.

Unique folders for simultaneously running tests are set via root.directory=target/results/#{T(java.util.UUID).randomUUID().toString()}. This property is injected as @Value in spring @Component class to avoid re-calculation root directory in different places.

The main issue is following: I need to prepare folders which name should be specified via application.properties with some resources(copy files, folders) in @Before test-section and run @PostConstruct only after all resource are prepared.

I tried several variants: 1) autowire in the test a bean with @PostConstruct and invoke it programmatically in the end of @Before - it's a single working case and it looks strange and fragile 2) Replace @PostConstruct with InitializingBean and afterPropertiesSet - it doesn't work. Because I have a value for folder name on bean initialization stage but without copied resources that I copy in @Before test section

I hope I explained well. I will be appreciate with any help or advice.

Upvotes: 1

Views: 1285

Answers (1)

Avinash
Avinash

Reputation: 21

Your Question are bit vague but to answer the issues.

The main issue is following: I need to prepare folders which name should be specified via application.properties with some resources(copy files, folders) in @Before test-section and run @PostConstruct only after all resource are prepared.

  1. Make Sure you are running Test
@RunWith(SpringRunner.class)
@SpringBootTest(properties = "myFileName=test.txt")
  1. In test @Before use
@Rule 
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
temporayFolder.newFile( use @Value here)
  1. So @Before will run before any spring boot startup and then call your function in one of the unit test methods

Upvotes: 1

Related Questions