Я тестирую в своем контроллере метод post, который возвращает только строку и использую Mockito для имитации вызова службы. Моя проблема в том, что когда метод службы вызывается на контроллере, он возвращает значение null.
@RunWith(SpringRunner.class)
@WebMvcTest(ProcessGroupController.class)
public class ProcessGroupRestControllerTest {
.............
@Test
public void givenAllNifiArguments_whenImportProcessGroup_thenReturnJsonOk() throws Exception {
NiFiArguments niFiArguments = NiFiArguments.builder()......flowVersion("3").build();
String expected = "1689d61b-624d-4574-823d-f1b4755882e1";
String json = mapper.writeValueAsString(niFiArguments);
//Mock service call
when(nifiService.importProcessGroup(niFiArguments)).thenReturn(expected);
mvc.perform(post("/nifi/pg-import").contentType(MediaType.APPLICATION_JSON).content(json))
.andExpect(status().isCreated())......);
}
Контроллер:
@PostMapping("/pg-import")
public ResponseEntity<String> importProcessGroup(@RequestBody NiFiArguments niFiArguments)
throws NiFiClientException {
log.info("Called method importFlow");
String result = nifiService.importProcessGroup(niFiArguments);
return new ResponseEntity<String>(result, HttpStatus.CREATED);
}
String result = null
У меня есть похожие тесты, которые возвращают POJO, и он отлично работает