简单的单元测试
对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;