diff --git a/gh-7174/pom.xml b/gh-7174/pom.xml new file mode 100644 index 0000000..0c30530 --- /dev/null +++ b/gh-7174/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + com.example + spring-boot-test-context-cache + 1.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.4.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + + 1.8 + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/gh-7174/src/main/java/example/FooBarApplication.java b/gh-7174/src/main/java/example/FooBarApplication.java new file mode 100644 index 0000000..c95aed8 --- /dev/null +++ b/gh-7174/src/main/java/example/FooBarApplication.java @@ -0,0 +1,35 @@ +package example; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; + +@SpringBootApplication +@RestController +public class FooBarApplication { + + @Autowired FooBarService service; + + public static void main(String[] args) { + SpringApplication.run(FooBarApplication.class, args); + } + + @GetMapping("/foo") + public String foo() { + return service.foo(); + } + + @GetMapping("/bar") + public void bar() { + service.bar(); + } + + @ExceptionHandler(RuntimeException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public void handleException() { + // do logging etc + } + +} diff --git a/gh-7174/src/main/java/example/FooBarService.java b/gh-7174/src/main/java/example/FooBarService.java new file mode 100644 index 0000000..b3c3e41 --- /dev/null +++ b/gh-7174/src/main/java/example/FooBarService.java @@ -0,0 +1,16 @@ +package example; + +import org.springframework.stereotype.Service; + +@Service +public class FooBarService { + + public String foo() { + return "foo"; + } + + public void bar() { + // do some bar stuff + } + +} diff --git a/gh-7174/src/main/resources/logback.xml b/gh-7174/src/main/resources/logback.xml new file mode 100644 index 0000000..1b185bb --- /dev/null +++ b/gh-7174/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/gh-7174/src/test/java/example/BarTest.java b/gh-7174/src/test/java/example/BarTest.java new file mode 100644 index 0000000..fbc2ce9 --- /dev/null +++ b/gh-7174/src/test/java/example/BarTest.java @@ -0,0 +1,29 @@ +package example; + +import org.junit.Test; +import org.junit.runner.RunWith; +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.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; + +@RunWith(SpringRunner.class) +@WebMvcTest +public class BarTest { + + @MockBean FooBarService service; + @Autowired MockMvc mockMvc; + + @Test + public void shouldReturnStatus500() throws Exception { + doThrow(new RuntimeException()).when(service).bar(); + + mockMvc.perform(get("/bar")) + .andExpect(status().isInternalServerError()); + } + +} diff --git a/gh-7174/src/test/java/example/FooTest.java b/gh-7174/src/test/java/example/FooTest.java new file mode 100644 index 0000000..3b3ce20 --- /dev/null +++ b/gh-7174/src/test/java/example/FooTest.java @@ -0,0 +1,29 @@ +package example; + +import org.junit.Test; +import org.junit.runner.RunWith; +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.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; + +@RunWith(SpringRunner.class) +@WebMvcTest +public class FooTest { + + @MockBean FooBarService service; + @Autowired MockMvc mockMvc; + + @Test + public void shouldReturnXYZ() throws Exception { + final String expected = "xyz"; + when(service.foo()).thenReturn(expected); + mockMvc.perform(get("/foo")) + .andExpect(content().string(expected)); + } + +}