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

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.

主站蜘蛛池模板: 枞阳县| 邹平县| 油尖旺区| 淮安市| 定边县| 阿拉尔市| 武功县| 呼图壁县| 隆尧县| 大宁县| 基隆市| 泽库县| 凤山县| 泗水县| 惠安县| 崇礼县| 乌恰县| 沙湾县| 封开县| 广平县| 临桂县| 新田县| 广灵县| 佛山市| 金山区| 思南县| 惠州市| 怀来县| 监利县| 杂多县| 隆昌县| 政和县| 晋中市| 荣昌县| 神池县| 宣城市| 隆安县| 卢龙县| 慈利县| 武隆县| 班玛县|