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

SpringBoot Starter Test

spring-boot-starter-test ist die Hauptabhängigkeit dieses Tests. Es enthält die meisten Elemente, die für den Test erforderlich sind.

Wir können verschiedene Arten von Tests schreiben, um die Funktionsfähigkeit und Automatisierung der Anwendung zu testen. Bevor wir mit jedem Test beginnen, müssen wir den Testrahmen integrieren.

Für Spring Boot müssen wir im Projekt hinzufügen starter Für den Test müssen wir nur hinzufügen spring-boot-starter-test Abhängigkeit.

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

Es zieht alle Abhängigkeiten, die mit dem Test verbunden sind, heraus. Nachdem sie hinzugefügt wurden, können wir einen einfachen Unit-Test erstellen. Wir können ein Spring Boot-Projekt durch eine IDE erstellen oder es mit Spring Initializr generieren.

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 the 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

步骤1: Open Spring Initializr https://start.spring.io/。

步骤2: provided Group name and artifact ID. We have provided the group name com.w3codebox and artifact spring-boot-test-example.

步骤3: Add

步骤4: 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.

步骤5: Extract the downloaded Jar file.

步骤6: Import the folder into STS. The import 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 STS Package Explorer section.

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() 
{
}
}

Default implementation of the above code 两个注释: @SpringBootTest, @Test。

@SpringBootTest: : 它适用于运行基于Spring Boot的测试的Test Class。除了常规的Spring TestContext Framework之外,它还提供以下功能: 如果未定义特定的@ContextConfiguration(loader = ...),它将使用 SpringBootContextLoader 作为默认的ContextLoader。 当不使用嵌套的@Configuartion并且未指定显式类时,它将自动搜索 @SpringBootConfiguration 它为不同的 WebEnvironment 模式提供支持。 它注册一个 TestRestTemplate 或WebTestClient bean,以便在使用Web服务器的Web测试中使用。 它允许使用 args属性定义应用程序参数。

步骤7: 打开 SpringBootTestExampleApplicationTest.java 文件,并以 以Junit Test的身份运行它。

运行上述代码时,它显示以下内容: