junit 单元测试指南

摘要:本文主要介绍如何使用junit来编写单元测试,包含mock对象的方法和mock静态方法使用案例,提高开发效率。

pom.xml引入依赖包

springboot引入依赖包

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

普通java项目

<!-- junit5 -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.2</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.9.2</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>4.11.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-junit-jupiter</artifactId>
    <version>4.11.0</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>4.11.0</version>
    <scope>test</scope>
</dependency>

定义的类

UserEntity

@Data
@TableName("user")
public class UserEntity {

    @TableId
    private String id;

    private String name;

    private String address;

    private Date createTime;
}

UserUtils

作为被mock的静态类

public class UserUtils {

    public static UserEntity getUser(){
        return new UserEntity();
    }

    public static boolean checkName(String name){
        UserEntity userEntity = getUser();
        return userEntity.getName().equals(name);
    }

}

UserService

mock的测试类

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @SDS("dynamic_member")
    @Transactional(rollbackFor = Exception.class)
    public UserEntity add(UserEntity userEntity) throws Exception{
        userEntity.setId(IdWorker.getIdStr());
        userEntity.setCreateTime(new Date());
        userMapper.insert(userEntity);
        return userEntity;
    }

    public UserEntity getLoginUser(){
        return UserUtils.getUser();
    }

    public boolean checkName(String name){
        boolean result = UserUtils.checkName(name);
        System.out.println(result);
        return result;
    }
}

单元测试

UserServiceTest

@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    /** mock静态类 */
    private static MockedStatic<UserUtils> userUtils;
    /** mock被测试的类 */
    @InjectMocks
    private UserService userService;

    /** mock注入的类 */
    @Mock
    private UserMapper userMapper;

    @BeforeAll
    static void init(){
        userUtils = Mockito.mockStatic(UserUtils.class);
    }

    @Test
    void add() throws Exception {
        // mock方法中调用的其他方法
        Mockito.when(userMapper.insert(Mockito.any())).thenReturn(1);
        UserEntity result = userService.add(new UserEntity());
        assertNotNull(result.getId());
    }

    @Test
    void getLoginUser() {
        UserEntity userEntity = new UserEntity();
        userEntity.setName("zhangsan");
        // mock无参静态方法
        userUtils.when(UserUtils::getUser).thenReturn(userEntity);
        UserEntity result = userService.getLoginUser();
        assertEquals("zhangsan",result.getName());
    }

    @Test
    void checkName() {
        // mock有参数静态方法
        userUtils.when(() -> UserUtils.checkName(Mockito.anyString())).thenReturn(true);
        boolean result = userService.checkName("zhangsan");
        assertTrue(result);

        // mock有参数静态方法
        userUtils.when(() -> UserUtils.checkName(Mockito.anyString())).thenReturn(false);
        result = userService.checkName("zhangsan");
        assertFalse(result);
    }
}

测试结果

符合我们得预期结果

image.png

image.png

全部评论

相关推荐

天天困啊:个人建议第一点就是熟悉Redis这里不要这么写,写上Redis比较核心的技术,什么缓存一致性,雪崩穿透击穿那些,掌握cos其实不用写在专业技能里这个你做了鱼皮的这个项目面试官默认应该认为你应该懂了,鱼皮这个项目核心挺多建议多啃啃,在做一个鱼皮的微服务项目俩项目在一起比较好哦
你的简历改到第几版了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务