0

I have a Spring MVC Application developed with Spring Boot. This is application is just for learning purposes, by the way.

By default, the app launches and uses a MySQL database. For unit and integration testing, I use an in-memory H2 database and it works perfectly.

So for that, I have two application.properties. One is under /src/main/resources/application.properties.

spring.datasource.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost/myDatabase
spring.datasource.username = root
spring.datasource.password = mysql

spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false

The other application.properties in under /src/test/resources/application.properties

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"
spring.datasource.username=sa
spring.datasource.password=sa

Now, I have to use Selenium for automated website testing and I don't want my MySQL database to be populated with test data.

I haven't done this previously in Spring, but I would like my application to work like this:

  • Launch from terminal my application with certain commands specifying what database it should use. It should launch on localhost:8080
  • And then, run all Selenium test in localhost:8080. All the data generated with Selenium tests is only kept in memory as long as the application is running

How to do this in a Spring Boot Application using an application.properties or other configuration?

GianMS
  • 923
  • 2
  • 13
  • 25
  • What's the problem with this approach? When you are running your test cases, you can load application.properties from /src/test/resources/application.properties and it should work for you. – asg Sep 20 '17 at 05:29
  • Have you read [this](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html)? Just specify System properties when starting the application `java -Dspring.datasource.url= -jar your.jar`. Or include profile specific property files and use `-Dspring.profiles.active=` before launching. – M. Deinum Sep 20 '17 at 05:29
  • Put your database settings in application-mysql.properties and application-h2.properties and run your application with -Dspring.profiles.active=mysql(or h2) – Azarea Sep 20 '17 at 06:01

2 Answers2

0

Spring should do this for you automatically. To run application.properties from src/test/resources when you are running tests because spring runs with "test" profile. If not, add @ActiveProfiles("test") annotation on your test class (and by that I mean the class where you have your tests, not the class under test). If even that doesn't work, you can rename your src/test/resources/application.properties to src/test/resources/application-test.properties and select your profile in your run configuration (there is a field called 'profile'). Reference and more info.

Urosh T.
  • 3,336
  • 5
  • 34
  • 42
0
  1. Create a separate properties file named application-test.properties and place it under /src/test/resources. The test database properties (or any other test specific properties) should go here.
  2. On top of your test class, use this annotation @ActiveProfiles("test").

    @ActiveProfiles("test")
    public class MyTest {
       ...
    }
    
Indra Basak
  • 7,124
  • 1
  • 26
  • 45