17

I am trying to run tests on a Spring Boot api with H2 database in the test, however, when trying to run the tests the system is using the application.properties in the main resource instead of the test. I tried naming the file as application-test.properties and use the annotation @ActiveProfiles("test") in the test class but this did not work (testing putting in the main/resources and then in test/resouces) Now I do not know what to try.

My main/resources/apllication.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/chamados
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

My test/resources/application.properties:

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=
spring.h2.console.enabled=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true

My test class that just runs:

@RunWith(SpringRunner.class)
@SpringBootTest
public class BackEndApplicationTests {
 @Test
 public void contextLoads() {
 }
}

My pom.xml:

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>1.8</java.version>
 <jwt.version>0.9.1</jwt.version>
</properties>
<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
 <groupId>com.h2database</groupId>
 <artifactId>h2</artifactId>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <scope>runtime</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-tomcat</artifactId>
 <scope>provided</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-test</artifactId>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.security</groupId>
 <artifactId>spring-security-test</artifactId>
 <scope>test</scope>
 </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-test</artifactId>
 <scope>test</scope>
 </dependency>
 <!-- Autenticação -->
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-security</artifactId>
 </dependency>
 <dependency>
 <groupId>io.jsonwebtoken</groupId>
 <artifactId>jjwt</artifactId>
 <version>${jwt.version}</version>
 </dependency>
</dependencies>
<build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
</build>
asked Dec 16, 2018 at 15:45

2 Answers 2

31

Spring boot at first and always loads application.properties then if exists application-{profile}. At this case the last one will override some properties from the parent (application.properties). Find more at spring-boot documentation.

You should have main/resource/application.properties and test/resource/application-test.properties (not application.properties at the test directory) + @ActiveProfiles("test"). Then it will work. If you think that is doesn't work - check classpath of running app. For example, idea + maven - target directory; idea + gradle - build directory.

veben
22.7k15 gold badges70 silver badges85 bronze badges
answered Dec 16, 2018 at 18:03
Sign up to request clarification or add additional context in comments.

4 Comments

You said "At this case the last one will override some properties from the parent" and that was the problem along with the question of the name applicatio-test.properties. In the application-test I had not set value for spring.datasource.driver-class-name, now works.
For me, that was exactly the issue. Had the property file for the test environment placed in the main/resources/ folder. Thank you!
This is not correct. You can have application.properties in the test/resources directory and spring will use it. If you use application-test.properties, then you need to use @ActiveProfiles("test"), which informs Spring that you're using the test profile.
@pyetti the thing is I recommend using profiles always because it's not intuitive to remember with application.properties file will be loaded first. YOu should separate it
10

Create another application file with name application-test.properties with the following content in same directory only no need to create under test:

spring.datasource.url = "jdbc:h2:~/testdb;DB_CLOSE_ON_EXIT=FALSE"
spring.datasource.username = sa
spring.datasource.password = 
spring.datasource.driverClassName = org.h2.Driver

Then add the following annotation to your test classes:

@ActiveProfiles("test")

This will work because in spring boot we can have several profiles so we are creating one profile with the name of the test.

answered Mar 16, 2020 at 1:46

1 Comment

Please have a go at formatting the code parts of your answer appropriately.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.