Linked Questions

Popular Questions

I need advice on what method will suit better for my case.

I have a Java application with Spring Boot, and right now, for the testing part, I use a localhost PostgreSQL database. The setup is quite simple; I just have the datasource.url/username/password and port setup in the application.properties file.

Now, I need to use a remote PostgreSQL database for a particular integration test class. So, as far as I know, I can do this in at least two ways:

  • I can set up in application.properties another configuration for the remote database, and use it in the test class with environment.getProperty().

  • Or, I can make a bean to use those properties:

    @Bean
    public DataSource dataSource() {
        DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
            dataSourceBuilder.url(dbUrl);
            dataSourceBuilder.username(username);
            dataSourceBuilder.password(password);
            return dataSourceBuilder.build();   
    }
    

And the things are not as simple as they may seem. I have a class for tests configuration, ClassA.

I have a second class with actual tests, let's call it ClassB.

ClassB extends Class A, and ClassA starts a JAR file, which is another Java application which exposes some rest APIs, which will be tested with the tests from ClassB.

Now I use a local database, but I will want to use the remote one, in the future.

I managed to download the JAR file from Artifactory with a Maven dependency, and in the configuration class I search for the JAR file, I get the directory of it, and use an array of commands to run it: {"java", "-jar", "directory.getCanonicalPath()}, and ProcessStreamer to process it.

After I check the JAR file is up and running, the tests from ClassB are triggered, and they do CRUD operations against the recently opened application making REST calls to the APIs.

Any suggestions on how I can set up the second remote database?

Thank you!

Related Questions