Код: Выделить всё
@RestController
public class MyController {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Autowired
private MyService service;
@GetMapping(path = "/perform")
public ResponseEntity perform() {
ResponseDTO response = null;
try {
response = service.perform();
} catch (WebClientResponseException.NotFound wcreNotFound) {
log.error("Could not fetch: ", wcreNotFound);
response = new ResponseDTO();
response.setInfo(..);
}
return new ResponseEntity(response, HttpStatus.OK);
}
Код: Выделить всё
@Test
public void testCondition() throws Exception {
when(service.perform()).thenThrow(WebClientResponseException.NotFound.class);
ResponseEntity result = controller.perform();
assertEquals(...);
}
Код: Выделить всё
Caused by: java.lang.NullPointerException: Cannot invoke
"org.springframework.http.HttpStatusCode.is1xxInformational()" because
"this.statusCode" is null
at org.springframework.web.reactive.function.client.WebClientResponseException.shouldHintAtResponseFailure(WebClientResponseException.java:285)
at org.springframework.web.reactive.function.client.WebClientResponseException.getMessage(WebClientResponseException.java:278)
at java.base/java.lang.Throwable.getLocalizedMessage(Throwable.java:397)
at java.base/java.lang.Throwable.toString(Throwable.java:496)
- Заставить Mockito имитировать исключение WebClientResponseException с правильным значением HttpStatus, заполненным в thenobject. Но это не работает:
Код: Выделить всё
when(service.perform()).thenThrow(new WebClientResponseException(404, null, null, null, null));
B. Невозможно также создать исключение NotFound — во всех случаях синтаксическая ошибка:
Код: Выделить всё
when(service.perform()).thenThrow(new WebClientResponseException.NotFound(404, null, null, null, null));
Код: Выделить всё
when(service.perform()).thenThrow(new WebClientResponseException.NotFound());
- Подавление ведения журнала:
Я не хочу уменьшать или удалять журналирование модульных тестов; регулярная регистрация из jUnits все равно должна происходить. Мне просто нужно подавить конкретную регистрацию того, что я моделирую и ожидаю здесь.
Подробнее здесь: https://stackoverflow.com/questions/798 ... g-springfr
Мобильная версия