单元测试:无返回值接口单元测试

在平常的工作中,我们经常会用到单元测试,那么,单元测试应该怎么写呢?有什么需要注意的地方呢?

比如保存,数据是否保存成功,我们应该用单元测试怎么断言呢?像保存完成后,再去数据库做一边查询,看数据是否保存成功,那么,除过这种,我们还可以用下面的这种方式.

1:引入包:

java 复制代码
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>2.0.2-beta</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.9</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>

2:service保存方法:

java 复制代码
package test.boot.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import test.boot.dao.StudentDao;
import test.boot.service.StudentService;
import test.boot.vo.StuentVO;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    public void save(StuentVO vo1) {
        studentDao.save(vo1);
    }

}

3:单元测试:

java 复制代码
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.powermock.api.mockito.PowerMockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import test.boot.SpringbootApplicationTest;
import test.boot.dao.StudentDao;
import test.boot.service.StudentService;
import test.boot.vo.StuentVO;

import static org.mockito.Mockito.verify;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootApplicationTest.class)
public class StudentTest {

    @Mock
    private StudentDao studentDao;

    @Mock
    private StudentService studentService;

    @Test
    public void test02(){
        StuentVO vo = new StuentVO();
        vo.setName("大杜");
        vo.setPhone("12345678911");
        vo.setId(1L);
        // 先调用保存方法
        studentService.save(vo);
        // 对入参进行获取
        ArgumentCaptor<StuentVO> stuentVOArgumentCaptor = ArgumentCaptor.forClass(StuentVO.class);
        verify(studentDao).save(stuentVOArgumentCaptor.capture());
        Assert.assertEquals("姓名不一致", vo.getName(), stuentVOArgumentCaptor.getValue().getName());
    }
}

4:像保存这些接口,无返回值,但是需要进行断言,看是否保存成功,我们mock保存方法,这样可以进行判断。

不断的学习,不断的充实自己,生活才会更加的美好!2024加油!美好的风景一直在路上!

相关推荐
.格子衫.5 小时前
Spring Boot 原理篇
java·spring boot·后端
多云几多5 小时前
Yudao单体项目 springboot Admin安全验证开启
java·spring boot·spring·springbootadmin
另寻沧海6 小时前
测试中的 AAA 模式与 Given–When–Then 模式详解
c++·单元测试·测试覆盖率
Jabes.yang8 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
聪明的笨猪猪8 小时前
Java Redis “高可用 — 主从复制”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
兮动人8 小时前
Spring Bean耗时分析工具
java·后端·spring·bean耗时分析工具
MESSIR228 小时前
Spring IOC(控制反转)中常用注解
java·spring
摇滚侠8 小时前
Spring Boot 3零基础教程,Demo小结,笔记04
java·spring boot·笔记
笨手笨脚の9 小时前
设计模式-迭代器模式
java·设计模式·迭代器模式·行为型设计模式
spencer_tseng10 小时前
Eclipse 4.7 ADT (Android Development Tools For Eclipse)
android·java·eclipse