while mocking the controller class, i am unable to cover the catch block in my unit testing,
here is the code - Controller Class -
public ResponseEntity<ResponseSentence> getText(@RequestBody Text text) throws IOException {
try {
return new ResponseEntity<>(serviceImpl.getText(text), HttpStatus.OK);
} catch (IOException e) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
this is my test class
@Autowired
private MockMvc mockMvc;
@MockBean
private Service service;
@MockBean
private ServiceImpl serviceImp;
@Autowired
private ObjectMapper objectMapper;
@Test
void getText() throws Exception {
Text text = new Text();
text.setText("");
//given(service.getText(text)).willThrow(new IOException("boom"));
when(serviceImp.getText(text)).thenThrow(new IOException());
ResultActions response = mockMvc.perform(post("/opennlp/v1/sentence")
.contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(text)));
response.andDo(print()).
andExpect(status().isBadRequest());
Mockito.verify(serviceImp).getText(any());
}
while code coverage the catch block is not covered.