как тут полностью замокать вызов octMsGatewayService.payOct(octId);? сейчас у меня…
- User
как тут полностью замокать вызов octMsGatewayService.payOct(octId);? сейчас у меня просто null возвращается @Override public void payOct(Long requestId) { Request request = dataService.selectFromWhere(QRequest.request, QRequest.class, r -> r.id.eq(requestId)).fetchFirst(); if (RequestStatus.CONFIRMED.name().equals(request.getStatus())) { OctId octId = new OctId(request.getExternalId()); OctResponse octResponse = octMsGatewayService.payOct(octId); if (octResponse.getSuccess()) {}
- Assistant
- User
у меня вот так но это не работает
@InjectMocks private OctMsGatewayServiceImpl octMsGatewayServiceImpl; @Mock private OctMsGatewayService octMsGatewayService;@Test void testPayOct() { OctId octId = new OctId(1L); OctResponse octResponseMock = new OctResponse(true, 200, new OctResponseDto()); Request requestMock = new Request(); requestMock.setStatus("CONFIRMED"); requestMock.setExternalId(1L); when(dataService.selectFromWhere(eq(request), eq(QRequest.class), any())).thenReturn(requestQuery); when(requestQuery.fetchFirst()).thenReturn(requestMock); when(transferPropertyService.getTransferPropertyValue(TransferProperty.KEY_OCT_MS_API_URL) + "pay").thenReturn("https://oct.co/api/pay"); when(octMsGatewayService.payOct(octId)).thenReturn(octResponseMock); operationServiceImpl.payOct(1L);
- Assistant
- User
- Assistant
Ошибка
java.lang.NullPointerException: Cannot invoke "acq.micro.acccardtransfer.model.dto.oct.OctResponse.getSuccess()" because "octResponse" is nullуказывает на то, что в методеpayOctпроисходит попытка обращения к методуgetSuccess()у объекта , который равен . Это значит, что вызов не возвращает ожидаемое значение. - User
этот метод находится в другом сервисе и выглядит так @Override public OctResponse payOct(OctId octId) { String fullUrl = transferPropertyService.getTransferPropertyValue(TransferProperty.KEY_OCT_MS_API_URL) + "pay"; return callOct(fullUrl, octId); }
private OctResponse callOct(String url, OctRequest commonOctRequest) { OctResponse octResponse = new OctResponse(); try { Response response = createInvocation(url, commonOctRequest).invoke(); int responseCode = response.getStatusInfo().getStatusCode(); log.info("result code {}", responseCode); String result = response.readEntity(String.class); if (StringUtils.isEmpty(result) || responseCode != 200) { if (StringUtils.isNotEmpty(result)) { log.info("result body {}", result); octResponse.setOctResponseDto(new Gson().fromJson(result, OctResponseDto.class)); octResponse.setResponseCode(responseCode); octResponse.setSuccess(false); return octResponse; } octResponse.setResponseCode(responseCode); octResponse.setSuccess(false); octResponse.setOctResponseDto(new OctResponseDto("" + responseCode, "Ошибка вызова OCT микросервиса")); return octResponse; } log.info("result body {}", result); octResponse.setResponseCode(responseCode); octResponse.setOctResponseDto(new Gson().fromJson(result, OctResponseDto.class)); octResponse.setSuccess(true); return octResponse; } catch (Exception e) { octResponse.setResponseCode(null); octResponse.setSuccess(false); octResponse.setOctResponseDto(new OctResponseDto("", "Ошибка вызова OCT микросервиса: " + e.getMessage())); return octResponse; } - Assistant
- User
у меня в этом методе стоит брейкоинт но я туда не попвдаю @Override public OctResponse payOct(OctId octId) { String fullUrl = transferPropertyService.getTransferPropertyValue(TransferProperty.KEY_OCT_MS_API_URL) + "pay"; return callOct(fullUrl, octId); }
- Assistant
- User
может в тесте еще и это callOct обработать надо? @Override public OctResponse payOct(OctId octId) { String fullUrl = transferPropertyService.getTransferPropertyValue(TransferProperty.KEY_OCT_MS_API_URL) + "pay"; return callOct(fullUrl, octId); }
private OctResponse callOct(String url, OctRequest commonOctRequest) { OctResponse octResponse = new OctResponse(); try { Response response = createInvocation(url, commonOctRequest).invoke(); int responseCode = response.getStatusInfo().getStatusCode(); log.info("result code {}", responseCode); String result = response.readEntity(String.class); if (StringUtils.isEmpty(result) || responseCode != 200) { if (StringUtils.isNotEmpty(result)) { log.info("result body {}", result); octResponse.setOctResponseDto(new Gson().fromJson(result, OctResponseDto.class)); octResponse.setResponseCode(responseCode); octResponse.setSuccess(false); return octResponse; } octResponse.setResponseCode(responseCode); octResponse.setSuccess(false); octResponse.setOctResponseDto(new OctResponseDto("" + responseCode, "Ошибка вызова OCT микросервиса")); return octResponse; } log.info("result body {}", result); octResponse.setResponseCode(responseCode); octResponse.setOctResponseDto(new Gson().fromJson(result, OctResponseDto.class)); octResponse.setSuccess(true); return octResponse; } catch (Exception e) { octResponse.setResponseCode(null); octResponse.setSuccess(false); octResponse.setOctResponseDto(new OctResponseDto("", "Ошибка вызова OCT микросервиса: " + e.getMessage())); return octResponse; } } - Assistant