Skip to content

Commit

Permalink
improve test code
Browse files Browse the repository at this point in the history
  • Loading branch information
pboos committed Nov 22, 2023
1 parent 06ef4a5 commit 55f127c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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())
Expand All @@ -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()),
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 55f127c

Please sign in to comment.