步骤1:添加依赖
确保在pom.xml
文件中添加了Spring Boot Test相关的依赖。如果你使用的是Spring Initializr生成的项目,这些依赖通常已经包含在内。如果没有,你可以手动添加:
<dependencies>
<!-- 其他依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
步骤2:编写测试类
创建一个用于测试的类,并使用Spring Boot提供的注解进行配置。通常测试类放在src/test/java
目录下,与被测试类相对应的包结构中。
例如,如果你有一个名为CalculatorService
的服务类:
package com.example.demo.service;
import org.springframework.stereotype.Service;
@Service
public class CalculatorService {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
}
那么你可以创建一个测试类CalculatorServiceTest
来测试它。
步骤3:使用Spring Boot Test注解
使用@SpringBootTest
注解来加载Spring应用程序上下文,并使用@Test
注解来定义测试方法。
package com.example.demo.service;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBootTest
public class CalculatorServiceTest {
@Autowired
private CalculatorService calculatorService;
@Test
public void testAdd() {
int result = calculatorService.add(1, 2);
assertEquals(3, result, "1 + 2 should equal 3");
}
@Test
public void testSubtract() {
int result = calculatorService.subtract(5, 3);
assertEquals(2, result, "5 - 3 should equal 2");
}
}
步骤4:运行测试
你可以通过多种方式运行测试:
- 在IDE中右键点击测试类或测试方法,选择运行。
- 使用Maven命令行运行:
mvn test
- 使用Gradle命令行运行:
./gradlew test
其他测试注解和工具
@MockBean
:用于创建和注入Mock对象。@WebMvcTest
:用于测试控制器层,加载最小的应用程序上下文。@DataJpaTest
:用于测试JPA持久层,加载嵌入式数据库和JPA相关配置。@TestConfiguration
:用于在测试类中定义特定的配置。
示例:测试控制器
假设有一个简单的控制器:
package com.example.demo.controller;
import com.example.demo.service.CalculatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CalculatorController {
@Autowired
private CalculatorService calculatorService;
@GetMapping("/add")
public int add(@RequestParam int a, @RequestParam int b) {
return calculatorService.add(a, b);
}
}
可以编写一个测试类来测试这个控制器:
package com.example.demo.controller;
import com.example.demo.service.CalculatorService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(CalculatorController.class)
public class CalculatorControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private CalculatorService calculatorService;
@Test
public void testAdd() throws Exception {
Mockito.when(calculatorService.add(1, 2)).thenReturn(3);
mockMvc.perform(get("/add").param("a", "1").param("b", "2"))
.andExpect(status().isOk())
.andExpect(content().string("3"));
}
}
以上示例展示了如何在Spring Boot中使用Spring Boot Test模块进行单元测试。可以根据具体需求选择合适的注解和工具来编写测试用例。