单元测试 - SpringBoot2+Mockito实战 (3)

service

package tech.pdai.springboot2unit5.service.impl; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.Mockito; import org.springframework.test.context.junit.jupiter.SpringExtension; import tech.pdai.springboot2unit5.entity.User; import java.util.Collections; import java.util.List; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @ExtendWith(SpringExtension.class) class UserServiceImplTest { @Mock private UserServiceImpl userService; @Test public void findAll() { //Given Mockito.when(userService.findAll()).thenReturn( Collections.singletonList(new User(1, "pdai.tech", "1221111"))); //When List<User> userDtoList = userService.findAll(); //Then assertFalse(userDtoList.isEmpty()); verify(userService, times(1)).findAll(); } }

测试结果

单元测试 - SpringBoot2+Mockito实战

Util测试

如果包含静态util的测试还可以加PowerMokito.

具体可以参考:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpsfzj.html