14

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/resource and then in test/resouce) Now I do not know what to try.

My main/resource/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/resource/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>
user2831852
  • 535
  • 2
  • 7
  • 22

2 Answers2

30

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
  • 19,637
  • 14
  • 60
  • 80
Artem Ptushkin
  • 1,151
  • 9
  • 17
  • 1
    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. – user2831852 Dec 16 '18 at 20:21
  • For me, that was exactly the issue. Had the property file for the test environment placed in the main/resources/ folder. Thank you! – vbknez Jul 17 '20 at 21:00
9

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.

keikai
  • 14,085
  • 9
  • 49
  • 68
Supreet Singh
  • 882
  • 12
  • 9