业务代码
@Slf4j
@Service
@RequiredArgsConstructor
public class UserHomeServiceImpl implements UserHomeService {
@Override
public UserInfoVO queryUserInfo(String userId, String type) throws Exception {
if ("1".equals(type)) {
if (StrUtil.isEmpty(userId)) {
throw new ValidateException("关注用户id不能为空!");
}
}
}
}
单元测试
**
* @Title:
* @Description:
* @Author: luohaihua
* @Date: 2024/03/14 17:36
* @Version: 1.0
*/
public class UserHomeServiceTest extends MockBaseTest {
@InjectMocks
public UserHomeServiceImpl UserHomeService;
@Mock
public UserHomeMapper userHomeMapper;
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
@Test(expected= ValidateException.class)//断言异常
public void testQueryUserInfoWithValidateException() throws Exception{
UserInfoVO userInfoVO = new UserInfoVO();
when(userHomeMapper.queryUserInfo(anyString(),anyString(),anyString(),anyString())).thenReturn(userInfoVO);
userHomeService.queryUserInfo("","1");
}
}