Код: Выделить всё
private MockHttpResponse request(final Method method, final String uri, final HttpServletRequest mockedRequest,
final Object service, final Optional content,
final Optional contentType,
final Optional, Object> contextObject : contextObjects.get().entrySet()) {
dispatcher.getDefaultContextObjects().put(contextObject.getKey(), contextObject.getValue());
pushContext(contextObject.getKey(), contextObject.getValue());
}
}
addMappings(dispatcher);
final MockHttpRequest request = performRequest(method, uri);
request.accept(MediaType.APPLICATION_JSON);
request.contentType(contentType.isPresent() ? contentType.get() : MediaType.APPLICATION_JSON_TYPE.toString());
if (cookies.isPresent()) {
for (Map.Entry cookieEntry : cookies.get().entrySet()) {
request.cookie(cookieEntry.getKey(), cookieEntry.getValue());
}
}
if (content.isPresent()) {
request.content(content.get());
}
final MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
return response;
}
@SuppressWarnings("unchecked")
public void pushContext(Class clasz, Object obj) {
Class type = (Class) clasz;
ResteasyProviderFactory.pushContext(type, (T) obj);
}
- dispatcher.getDefaultContextObjects().put(contextObject .getKey(), contextObject.getValue());
- RestaasyProviderFactory.pushContext(type, (T) obj);
И моя конечная точка отдыха:
Код: Выделить всё
public void updatePassword(@PathParam(value = "panelist") String panelistId,
@Context Optional authenticatedPanelist,
CurrentAndNewCredentialsDto dto)
throws ForbiddenException, InvalidArgumentException, UnexpectedException {
assertAuthenticatedUserHasAccessToPanelistResource(authenticatedUser, panelistId);
CurrentAndNewPassword currentAndNewPassword =
CurrentAndNewCredentialsDtoMapper.INSTANCE.toPasswordModel(dto);
Try result = facade.updatePassword(panelistId, currentAndNewPassword);
if (result.isFailure()) {
Throwable failure = result.getFailure();
if (failure instanceof InvalidArgumentException) {
logger.warn("Failure when panelist tries to updatePassword, for panelist {}", panelistId, failure);
throw (InvalidArgumentException) failure;
} else {
throw new UnexpectedException(failure);
}
}
}
Я попробовал два метода, как вы можете видеть в коде:
- dispatcher.getDefaultContextObjects().put(contextObject.getKey(), contextObject.getValue());
RestaasyProviderFactory.pushContext(type, (T) obj);< /li>
Подробнее здесь: https://stackoverflow.com/questions/783 ... -unit-test