spring boot下进行单元测试

本文介绍了在Spring Boot中使用JUnit4进行单元测试的方法,包括环境配置、常用注解如@RunWith、@SpringBootTest、@Transactional、@Before、@After、@BeforeEach、@AfterEach的使用,以及@MockBean和@SpyBean的差异。此外,还展示了如何利用MockMVC进行Controller的单元测试,无需实际启动项目即可完成测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

spring boot下进行单元测试

为了提高自己的代码质量,减少低级错误,降低代码验证的成本,特地学习在spring boot下的junit4及spring test的单元测试方法。

环境配置

spring boot中只需要加入以下maven配置即可开始使用junit4及spring test。

maven配置如下:

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

常用注解

@Test标记在测试方法上。
@RunWith是junit提供的运行平台扩展接口,在spring boot中,如果想用spring环境,需要使用@RunWith(SpringRunner.class)
@ContextConfigration用来指定spring的基础配置,可以是xml文件,也可以是java文件。
@SpringBootTest是spring boot提供的注解,他会自动寻找项目下的springboot启动配置项,不用再自己配置。
@Transactional注解能够使测试方法中所有事务操作在测试操作完成后进行回滚,避免对环境造成垃圾数据。
@Before,@After注解的方法可以在真实的测试方法执行前后执行,与spring mvc的拦截器类似。注意是每一个@Test方法前后都会执行。
@BeforeClass,@AfterClass注解的方法在测试类初始化时及测试类中所有测试方法执行完毕后执行,与@Before,@After有区别。

@MockBean||@SpyBean

开发测试环境可能会缺失一些功能,需要进行mock数据。比如依赖的接口没有测试环境,这时候测试的方法中依赖这个接口,就可以mock这个接口的数据。@MockBean和@SpyBean都可以进行数据mock。
@MockBean的使用方法

@Service
@Slf4j
public class TestServiceDefaultImpl implements TestService {

    @Override
    public String test(String arg1, int arg2) {
        return arg1 + arg2;
    }

    @Override
    public String testSay(String arg1, int arg2) {
        return "testSay" + arg1 + arg2;
    }
}
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestControllerTest {

 

    @MockBean
    private TestService testService;

    @Before
    public void before() {
        Mockito.when(testService.test("1", 2)).thenReturn("mockBean生效啦");

    }

    @Test
    public void testService() {
        System.out.println("test方法返回:"+testService.test("1", 2));
        Assert.assertEquals(testService.testSay("1", 2),"testSay12");
    }

} 

测试结果为:

test方法返回:null

org.junit.ComparisonFailure: 
Expected :mockBean生效啦
Actual   :testSay12

可以看到已经修改了service的原始方法,但是test方法返回的是null, @MockBean中的方法默认全部返回null,只有专门配置的方法会返回你想要的结果。

@SpyBean的使用方法

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestControllerTest {

    @SpyBean
    private TestService testService;

    @Before
    public void before() {
        Mockito.doReturn("spybean生肖啦").when(testService).testSay("1", 2);
    }
  
    @Test
    public void testService() {
        System.out.println("test方法返回:"+testService.test("1", 2));
        Assert.assertEquals(testService.testSay("1", 2),"testSay12");

    }


} 

返回结果:

test方法返回:12

org.junit.ComparisonFailure: 
Expected :spybean生肖啦
Actual   :testSay12

@SpyBean里所有的方法执行原有逻辑,但是可以指定方法返回自己想要的东西。一般情况下,@SpyBean更好用。

MockMVC

controller的单元测试可以使用MockMVC,不在需要自己启动项目,再去使用浏览器或者postman调用。
代码例子:

@RestController
@RequestMapping("/")
public class TestController {

    @Autowired
    private TestService testService;


    @GetMapping("/test")
    @TestAnno(age = "15",name = "www")
    public String test(String arg1, int arg2) {
        return testService.test(arg1, arg2);
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestControllerTest {

    private MockMvc mockMvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Before
    public void before() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }


    @Test
    public void testTest() throws Exception {
        MvcResult result = mockMvc.perform(get("/test/?arg1=1&arg2=2")).andExpect(status().isOk()).andReturn();

        Assert.assertEquals(result.getResponse().getContentAsString(), "12");
    }
} 

测试通过,这样可以连带拦截器一起测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值