JAVA单元测试入门

简单的单元测试

对Service中的findUser方法的单元测试

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
//ConsumerController.java
@RestController
@Slf4j
public class ConsumerController {
    @Autowired
    ConsumerService consumerService;
    @GetMapping("/findUser/{name}")
    public String findUser(@PathVariable(value = "name") String name){
        User user = consumerService.findUser(name);
        log.info("the resule is: {}",user.toString());
        return "ok";
    }
}

//ConsumerService.java
public interface ConsumerService {
    User findUser(String name);
}

//ConsumerServiceImpl.java
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    ConsumerMapper consumerMapper;
    @Override
    public User findUser(String name) {
        return consumerMapper.findUser(name);
    }
}

//ConsumerMapper.java
public interface ConsumerMapper {
    User findUser(String name);
}

//ConsumerMapperImpl.java
@Repository
public class ConsumerMapperImpl implements ConsumerMapper {
    @Override
    public User findUser(String name) {
        User zhangsan = new User("zhangsan", "123456");
        return zhangsan;
    }
}

//User.java
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private String name;
    private String password;
}

写Test类

public class ConsumerTest {
    @Test
    public void testfindUser(){
        //give 创建Mock对象
        ConsumerMapper mock = Mockito.mock(ConsumerMapper.class);

        //when 定义ConsumerMapper行为
        User user = new User("lishi", "8888");
        Mockito.when(mock.findUser("haha")).thenReturn(user);

        //mock ConsumerService
        ConsumerService consumerService = Mockito.mock(ConsumerService.class);
        User consumerServiceUser = consumerService.findUser("haha");
        //assert
        Assert.assertEquals(user,consumerServiceUser);
    }
}

在这里插入图片描述

结果显示实际模拟的是null,原因是没有将依赖关系模拟
解决方法:1、使用@Mock和@InjectMock自动注入依赖关系
2、使用

方法2:

public class ConsumerService {
    private ConsumerMapper consumerMapper;

    public ConsumerService(ConsumerMapper consumerMapper) {
        this.consumerMapper = consumerMapper;
    }

    // 其他方法...
}

测试类:

// 创建Mock对象
ConsumerMapper mock = Mockito.mock(ConsumerMapper.class);

// 定义ConsumerMapper行为
User user = new User("lishi", "8888");
Mockito.when(mock.findUser("haha")).thenReturn(user);

// 创建ConsumerService并传入Mock对象
ConsumerService consumerService = new ConsumerService(mock);

Mock的单元测试

方法1:使用@Mock和@InjectMock自动注入依赖关系

将ConsumerMapper mock = Mockito.mock(ConsumerMapper.class);使用@Mock来mock对象

@RunWith(MockitoJUnitRunner.class)
public class ConsumerTest {

    @Mock
    ConsumerMapper consumerMapper;

    @InjectMocks
    ConsumerServiceImpl consumerService;
    @Test
    public void testfindUser(){
        //when 定义ConsumerMapper行为
        User user = new User("lishi", "8888");
        Mockito.when(consumerMapper.findUser("haha")).thenReturn(user);
        //
        User consumerServiceUser = consumerService.findUser("haha");
        //assert
        Assert.assertEquals(user,consumerServiceUser);
    }
}

注意:报空指针异常,使用@Mock是需要初始化,可以使用@RunWith(MockitoJUnitRunner.class)自动初始化
或者public void setup() { MockitoAnnotations.initMocks(this); }

在这里插入图片描述

总结:1、Mockito.when(consumerMapper.findUser(“haha”)).thenReturn(user);是自己定于结果,只要调用了这个consumerMapper.findUser方法就返回自定义的user结果;
2、要测试的方法使用@InjectMocks,要定义的行为使用@Mock;

使用PowerMock模拟静态方法

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值