官术网_书友最值得收藏!

Testing in Spring Boot

The Spring Boot test starter package is added to pom.xml by Spring Initializr when we created our project. That 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 lot of handy libraries for testing, such as JUnit, Mockito, AssertJ, and more. If you 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 by adding the following dependency to the pom.xml file. The scope defines that the H2 database will be used only 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.

主站蜘蛛池模板: 视频| 左贡县| 商都县| 东至县| 邓州市| 兰考县| 淮滨县| 龙泉市| 连城县| 建平县| 浑源县| 五华县| 定南县| 稷山县| 竹北市| 安顺市| 汝城县| 建平县| 雷山县| 莫力| 昆明市| 诸暨市| 巴中市| 历史| 青州市| 科技| 广水市| 积石山| 亳州市| 盐津县| 大姚县| 林甸县| 厦门市| 蒙阴县| 伊春市| 通化市| 焦作市| 鹤峰县| 栖霞市| 湖南省| 德化县|