English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SpringBoot Starter Test

spring-boot-starter-test is the main dependency for the test. It contains most of the elements needed for testing.

We can write several different types of tests to help test and automate the operation of the application. Before starting any test, we need to integrate the test framework.

For Spring Boot, we need to add it to the project starter For testing, we just need to add spring-boot-starter-test dependency.

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

It pulls out all dependencies related to the test. After adding them, we can build a simple unit test. We can create a Spring Boot project through an IDE, or generate it using Spring Initializr.

Note: If you need to manually add test dependencies, please add them to the bottom of the pom.xml file.

One thing to note about the above dependencies is that it includes test scope <scope>test</scope>.By bundling and packaging for deployment, any dependencies declared with the test scope will be ignored. Test scope dependencies are only available when running in development and Maven test mode.

By default, when we create a simple Spring Boot application, it includes test dependencies in the pom.xml file src/test/javain the folder under ApplicationNameTest.java file.

We create a simple Maven project.

SpringBoot Starter Test example

Steps1: Open Spring Initializr https://start.spring.io/.

Steps2: provided Group name and artifact ID. We provide the group name com.w3codebox and artifact spring-boot-test-example.

Steps3: Add

Steps4: Click Generatebutton. When we click the "Generate" button, it will package all the specifications related to the project and Jar The file is downloaded to our local system.

Steps5: Extract the downloaded Jar file.

Steps6: Import the folder into STS. The import process may take some time.

File-> Import-> Existing Maven project-> Browse->Select folder spring-boot-test-example->Complete

After importing the project, we can see the following project directory in the Package Explorer part of STS.

We can see in the above directory that it contains a file named SpringBootTestExampleApplicationTest.java The test file is located in src/test/in the java folder.

SpringBootTestExampleApplicationTest.java

package com.w3codebox.springboottestexample;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringBootTestExampleApplicationTests 
{
@Test
void contextLoads() 
{
}
}

The default implementation of the above code twoAnnotation: @SpringBootTest,and @Test.

@SpringBootTest: : It is suitable for running Test Classes based on Spring Boot. In addition to the regular Spring TestContext Framework, it also provides the following features: If a specific @ContextConfiguration(loader = ...) is not defined, it will use SpringBootContextLoader as the default ContextLoader. When not using nested @Configuration and no explicit class is specified, it will automatically search @SpringBootConfiguration . It provides for different WebEnvironment pattern to provide support. It registers a TestRestTemplate or WebTestClient bean for using web server web testing. It allows the use of args attribute defines application parameters.

Steps7: Open SpringBootTestExampleApplicationTest.java file, and with Running it with Junit Test identity.

When running the above code, it displays the following content: