0

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:

  1. 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.

  2. How to re-connect JdbcTemplate using updated configuration?

Artur
  • 336
  • 2
  • 15
  • checkout below link : https://stackoverflow.com/questions/30337582/spring-boot-configure-and-use-two-datasources have a mapping for which branch to use which datasource , then proceed with that – saravana Dec 24 '20 at 17:14

0 Answers0