Having following code:
@SpringBootTest(classes = UiApplication.class)
class CleanTestData {
@Autowired DataPopulation dataPopulation;
List<String> branches = Arrays.asList("master","f1", "f2");
@Test
void cleanData() {
for (String branch : branches){
dataPopulation.removeAllTestData();
}
}
}
@Component
@Slf4j
public class DataPopulation {
private JdbcTemplate jdbcTemplate;
@Autowired
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void removeAllTestData() {
jdbcTemplate.update("delete from myaccounts")
}
}
application.properties
spring.datasource.url=jdbc:postgresql://8.8.8.8:7320/${branch}
Each branch has its own db.
During cleanData() test run, it needs to re-connect to specific db.
Files:
application.properties - Spring configuration
DataPopulation - class for connecting to db and execute some query
CleanTestData - test, which call clean test data for each branch.
There are following challenges, that I need to solve:
How to pass branch name to spring.datasource.url, while cleanData() test is running?
Probably, I can put variable of the branch as system environment variable and each time to override it.How to re-connect JdbcTemplate using updated configuration?