From 71f62fd7861680c9ede60cedaf213a83795a5da2 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 3 Oct 2020 17:07:00 +0200 Subject: [PATCH] Added expectations according the result of the request --- .../juplo/demo/ExceptionHandlingApplicationTests.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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()); } -- 2.20.1