springboot单元测试

步骤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模块进行单元测试。可以根据具体需求选择合适的注解和工具来编写测试用例。

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值