From 55f127cc1b666623e401937312b7ac0ed033062a Mon Sep 17 00:00:00 2001 From: Patrick Boos Date: Wed, 22 Nov 2023 14:43:09 +0100 Subject: [PATCH] improve test code --- .../ExceptionsNoExceptionHandlerTest.java | 13 +++-------- .../ExceptionsWithExceptionHandlerTest.java | 6 +++-- .../SpringBootTestConfiguration.java | 7 ++---- .../DefaultSpringBootTestConfiguration.java | 23 +++++++++++++++++++ 4 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 test/test-utils/src/main/java/com/getyourguide/openapi/validation/test/DefaultSpringBootTestConfiguration.java diff --git a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsNoExceptionHandlerTest.java b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsNoExceptionHandlerTest.java index 2896c22..6778be2 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsNoExceptionHandlerTest.java +++ b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsNoExceptionHandlerTest.java @@ -18,7 +18,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.web.servlet.MockMvc; @@ -43,7 +42,7 @@ public void setup() { @Test public void whenTestInvalidQueryParamThenReturns400WithoutViolationLogged() throws Exception { - mockMvc.perform(get("/test").queryParam("date", "not-a-date").contentType(MediaType.APPLICATION_JSON)) + mockMvc.perform(get("/test").queryParam("date", "not-a-date")) .andExpectAll( status().is4xxClientError(), content().string(Matchers.blankOrNullString()) @@ -61,10 +60,7 @@ public void whenTestInvalidQueryParamThenReturns400WithoutViolationLogged() thro public void whenTestThrowExceptionWithResponseStatusThenReturns400WithoutViolationLogged() throws Exception { mockMvc - .perform( - get("/test").queryParam("testCase", "throwExceptionWithResponseStatus") - .contentType(MediaType.APPLICATION_JSON) - ) + .perform(get("/test").queryParam("testCase", "throwExceptionWithResponseStatus")) .andExpectAll( status().is4xxClientError(), content().string(Matchers.blankOrNullString()), @@ -86,10 +82,7 @@ public void whenTestThrowExceptionWithoutResponseStatusThenReturns500WithoutViol throws Exception { var exception = assertThrows(ServletException.class, () -> { mockMvc - .perform( - get("/test").queryParam("testCase", "throwExceptionWithoutResponseStatus") - .contentType(MediaType.APPLICATION_JSON) - ) + .perform(get("/test").queryParam("testCase", "throwExceptionWithoutResponseStatus")) .andExpect(status().is5xxServerError()); }); Thread.sleep(100); diff --git a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsWithExceptionHandlerTest.java b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsWithExceptionHandlerTest.java index 6af4fd5..ead6545 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsWithExceptionHandlerTest.java +++ b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/ExceptionsWithExceptionHandlerTest.java @@ -25,8 +25,10 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; -@SpringBootTest(classes = {SpringBootTestConfiguration.class, - ExceptionsWithExceptionHandlerTest.ExceptionHandlerConfiguration.class}) +@SpringBootTest(classes = { + SpringBootTestConfiguration.class, + ExceptionsWithExceptionHandlerTest.ExceptionHandlerConfiguration.class, +}) @AutoConfigureMockMvc @ExtendWith(SpringExtension.class) public class ExceptionsWithExceptionHandlerTest { diff --git a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/SpringBootTestConfiguration.java b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/SpringBootTestConfiguration.java index 7ea7cd2..6b3e6ae 100644 --- a/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/SpringBootTestConfiguration.java +++ b/spring-boot-starter/spring-boot-starter-web/src/test/java/com/getyourguide/openapi/validation/integration/SpringBootTestConfiguration.java @@ -1,6 +1,7 @@ package com.getyourguide.openapi.validation.integration; import com.getyourguide.openapi.validation.api.log.ViolationLogger; +import com.getyourguide.openapi.validation.test.DefaultSpringBootTestConfiguration; import com.getyourguide.openapi.validation.test.TestViolationLogger; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter; @@ -16,9 +17,5 @@ @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) -public class SpringBootTestConfiguration { - @Bean - public ViolationLogger testViolationLogger() { - return new TestViolationLogger(); - } +public class SpringBootTestConfiguration extends DefaultSpringBootTestConfiguration { } diff --git a/test/test-utils/src/main/java/com/getyourguide/openapi/validation/test/DefaultSpringBootTestConfiguration.java b/test/test-utils/src/main/java/com/getyourguide/openapi/validation/test/DefaultSpringBootTestConfiguration.java new file mode 100644 index 0000000..d23dde2 --- /dev/null +++ b/test/test-utils/src/main/java/com/getyourguide/openapi/validation/test/DefaultSpringBootTestConfiguration.java @@ -0,0 +1,23 @@ +package com.getyourguide.openapi.validation.test; + +import com.getyourguide.openapi.validation.api.log.ViolationLogger; +import org.springframework.boot.SpringBootConfiguration; +import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.FilterType; + +@SpringBootConfiguration +@EnableAutoConfiguration +@ComponentScan(excludeFilters = { + @ComponentScan.Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), + @ComponentScan.Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) +}) +public class DefaultSpringBootTestConfiguration { + @Bean + public ViolationLogger testViolationLogger() { + return new TestViolationLogger(); + } +}