Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tests in Spring Boot with database H2

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>
like image 652
user2831852 Avatar asked Dec 16 '18 15:12

user2831852


People also ask

How do I query H2 in spring boot?

H2 Console: By default, the console view of the H2 database is disabled. Before accessing the H2 database, we must enable it by using the following property. Once we have enabled the H2 console, now we can access the H2 console in the browser by invoking the URL http://localhost:8082/h2-console.


2 Answers

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.

like image 190
Artem Ptushkin Avatar answered Oct 06 '22 03:10

Artem Ptushkin


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.

like image 34
Supreet Singh Avatar answered Oct 06 '22 03:10

Supreet Singh