【发布时间】:2019-09-28 22:33:03
【问题描述】:
这是我的服务:
@Service
public class UserInfoService {
@Autowired
private UserInfoServiceClient UserInfoServiceClient; // Call another Rest API
public ResponseEntity<ResponseUserInfoData> sendUserInfo(String UserId) throws RuntimeException {
ResponseUserInfoData responseUserInfoData = new ResponseUserInfoData();
//Get the body from the User service client
UserServiceDTO UserServiceDTO = UserInfoServiceClient.sendResponse(UserId).getBody();
//Set the values of responseUserInfoData
Optional<UserServiceDTO> UserServiceDTOOptional = Optional.ofNullable(UserServiceDTO);
if (UserServiceDTOOptional.isPresent()) {
UserServiceDTOOptional.map(UserServiceDTO::getId).ifPresent(responseUserInfoData::setid);
}
else return ResponseEntity.noContent().build();
}
}
我必须测试它。我是 JUnit 测试的新手。我想测试以下几点:
检查服务是否返回响应实体
检查get和set方法是否有效
这是我开始的?
@RunWith(MockitoJUnitRunner.class)
public class ServiceTests {
@InjectMocks
private UserInfoService UserInfoService;
@Mock
private UserInfoServiceClient UserInfoServiceClient;
@Mock
private UserServiceDTO UserServiceDTO;
@Test
public void shouldReturnUserInfoData() throws IOException{
UserInfoService.sendUserInfo("ABC");
}
}
有什么帮助吗?
【问题讨论】:
标签: java spring-boot unit-testing junit