From: Kai Moritz Date: Sat, 3 Oct 2020 15:07:00 +0000 (+0200) Subject: Added expectations according the result of the request X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=71f62fd7861680c9ede60cedaf213a83795a5da2;p=demos%2Ftesting Added expectations according the result of the request --- diff --git a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java index 77eb7f6..67a9e76 100644 --- a/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java +++ b/src/test/java/de/juplo/demo/ExceptionHandlingApplicationTests.java @@ -5,9 +5,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; import java.net.URI; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -27,9 +29,13 @@ class ExceptionHandlingApplicationTests { void test400ForExceptionInBusinessLogic() throws Exception { when(service.checkAnswer(anyInt())).thenThrow(new IllegalArgumentException("FOO!")); - mvc + MvcResult result = mvc .perform(get(URI.create("http://FOO/?answer=1234"))) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()) + .andReturn(); + + assertThat(result.getModelAndView().getViewName()).isEqualTo("400"); + assertThat(result.getResolvedException()).isInstanceOf(IllegalArgumentException.class); verify(service, times(1)).checkAnswer(anyInt()); }