Testing in Spring Boot

The Spring Boot test starter package is added to pom.xml by Spring Initializr when we create our project. This is added automatically without any selection in the Spring Initializr page:

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

The Spring Boot test starter provides lots of handy libraries for testing, such as JUnit, Mockito, and AssertJ. If you take a look, your project structure already has its own package created for test classes:

By default, Spring Boot uses an in-memory database for testing. We are now using MariaDB, but H2 can also be used for testing if we add the following dependency to the pom.xml file. The scope defines that the H2 database will only be used for running tests; otherwise, the application will use the MariaDB database:

    <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>

If you also want to use the default database for testing, you can use the @AutoConfigureTestDatabase annotation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.84.29