Я пишу тестовые примеры j-unit для своих сервисов, в которых я не смогла правильно смоделировать ответ службы, что дает мне нулевое значение.Может ли кто-нибудь помочь мне в этом вопросе.
public ResponseEntity<Void> lockGet(
@ApiParam(value = "Unique identifier for this request.", required = true) @RequestHeader(value = "service-id", required = true) String serviceId,
@ApiParam(value = "Logged in userid.", required = true) @RequestHeader(value = "user-id", required = true) String userId,
@ApiParam(value = "Unique messageid.", required = true) @RequestHeader(value = "message-id", required = true) String messageId,
@RequestHeader(value = "access-token", required = true) String accessToken,
@ApiParam(value = "Unique id of the doamin of the entity", required = true) @RequestParam(value = "lockDomainId", required = true) Long lockDomainId,
@ApiParam(value = "Unique id of the entity to be fetched", required = true) @RequestParam(value = "lockEntityId", required = true) Long lockEntityId,
HttpServletRequest request, HttpServletResponse response) {
ResponseEntity<Void> result = null;
if (request.getAttribute("user-id") != null)
userId = (String) request.getAttribute("user-id");
String logContext = "||" + lockDomainId + "|" + lockEntityId + "||";
ThreadContext.put("context", logContext);
long t1 = System.currentTimeMillis();
LOG.info("Method Entry: lockGet" + logContext);
ServiceRequest serviceRequest = AppUtils.mapGetRequestHeaderToServiceRequest(serviceId, userId, lockDomainId,
lockEntityId);
try {
ServiceResponse serviceResponse = lockService.getLock(serviceRequest);
// set all the response headers got from serviceResponse
HeaderUtils.setResponseHeaders(serviceResponse.getResponseHeaders(), response);
result = new ResponseEntity<Void>(HeaderUtils.getHttpStatus(serviceResponse));
} catch (Exception ex) {
LOG.error("Error in lockGet", ex);
result = new ResponseEntity<Void>(HttpStatus.INTERNAL_SERVER_ERROR);
}
ThreadContext.put("responseTime", String.valueOf(System.currentTimeMillis() - t1));
LOG.info("Method Exit: lockGet");
return result;
}
@Test
public void testLockGetForError() {
request.setAttribute("user-id","TestUser");
ServiceRequest serviceRequest = new ServiceRequest();
serviceRequest.setUserId("TestUser");
ServiceResponse serviceResponse = new ServiceResponse();
LockService service = Mockito.mock(LockService.class);
when(service.getLock(serviceRequest)).thenReturn(serviceResponse);
// ServiceResponse serviceResponse = lockService.getLock(serviceRequest);
ResponseEntity<Void> result = new ResponseEntity<Void>(HeaderUtils.getHttpStatus(serviceResponse));
ResponseEntity<Void> lockGet = lockApiController.lockGet("1234", "TestUser", "TestMessage", "TestTkn", 12345L, 12345L, request, response);
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, lockGet.getStatusCode());
}
Я пробовал в другом сценарии, который не мог решить эту проблему.Кто-нибудь может мне помочь.Заранее спасибо.