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

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.

主站蜘蛛池模板: 仲巴县| 台江县| 新邵县| 汉源县| 泰兴市| 阳东县| 报价| 定结县| 富川| 阿鲁科尔沁旗| 靖江市| 北海市| 东乡族自治县| 廉江市| 渭源县| 秀山| 白沙| 庆阳市| 汕头市| 云龙县| 独山县| 嘉黎县| 措美县| 宜章县| 澄城县| 宁明县| 扶沟县| 淄博市| 南京市| 白水县| 济宁市| 广昌县| 泽州县| 达日县| 安远县| 昔阳县| 荆门市| 新绛县| 龙南县| 聂拉木县| 林口县|