From 282fabc58b400f813b33d1ac1888e71c12652fc7 Mon Sep 17 00:00:00 2001 From: Aleksander Bartnikiewicz Date: Tue, 25 Oct 2016 21:55:16 +0200 Subject: [PATCH 1/2] spring version bumped to 4.3.4-SNAPSHOT --- gh-7174/pom.xml | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/gh-7174/pom.xml b/gh-7174/pom.xml index 0c30530..20c32a2 100644 --- a/gh-7174/pom.xml +++ b/gh-7174/pom.xml @@ -13,6 +13,11 @@ 1.4.1.RELEASE + + 1.8 + 4.3.4.BUILD-SNAPSHOT + + org.springframework.boot @@ -25,17 +30,22 @@ - - 1.8 - - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-snapshot + Spring Portfolio Snapshot Repository + http://maven.springframework.org/snapshot + + \ No newline at end of file From 351442b92740dda476599edbde9d817cc88e77c5 Mon Sep 17 00:00:00 2001 From: Aleksander Bartnikiewicz Date: Tue, 25 Oct 2016 22:36:10 +0200 Subject: [PATCH 2/2] more complex example --- .../{FooBarService.java => BarService.java} | 6 +---- .../main/java/example/FooBarApplication.java | 7 +++--- gh-7174/src/main/java/example/FooService.java | 12 ++++++++++ gh-7174/src/test/java/example/BarTest.java | 10 ++++++-- gh-7174/src/test/java/example/FooTest.java | 10 ++++++-- .../test/java/example/TestConfiguration.java | 23 +++++++++++++++++++ 6 files changed, 56 insertions(+), 12 deletions(-) rename gh-7174/src/main/java/example/{FooBarService.java => BarService.java} (63%) create mode 100644 gh-7174/src/main/java/example/FooService.java create mode 100644 gh-7174/src/test/java/example/TestConfiguration.java diff --git a/gh-7174/src/main/java/example/FooBarService.java b/gh-7174/src/main/java/example/BarService.java similarity index 63% rename from gh-7174/src/main/java/example/FooBarService.java rename to gh-7174/src/main/java/example/BarService.java index b3c3e41..1010ea6 100644 --- a/gh-7174/src/main/java/example/FooBarService.java +++ b/gh-7174/src/main/java/example/BarService.java @@ -3,11 +3,7 @@ import org.springframework.stereotype.Service; @Service -public class FooBarService { - - public String foo() { - return "foo"; - } +public class BarService { public void bar() { // do some bar stuff diff --git a/gh-7174/src/main/java/example/FooBarApplication.java b/gh-7174/src/main/java/example/FooBarApplication.java index c95aed8..e1fb76f 100644 --- a/gh-7174/src/main/java/example/FooBarApplication.java +++ b/gh-7174/src/main/java/example/FooBarApplication.java @@ -10,7 +10,8 @@ @RestController public class FooBarApplication { - @Autowired FooBarService service; + @Autowired FooService fooService; + @Autowired BarService barService; public static void main(String[] args) { SpringApplication.run(FooBarApplication.class, args); @@ -18,12 +19,12 @@ public static void main(String[] args) { @GetMapping("/foo") public String foo() { - return service.foo(); + return fooService.foo(); } @GetMapping("/bar") public void bar() { - service.bar(); + barService.bar(); } @ExceptionHandler(RuntimeException.class) diff --git a/gh-7174/src/main/java/example/FooService.java b/gh-7174/src/main/java/example/FooService.java new file mode 100644 index 0000000..44046ba --- /dev/null +++ b/gh-7174/src/main/java/example/FooService.java @@ -0,0 +1,12 @@ +package example; + +import org.springframework.stereotype.Service; + +@Service +public class FooService { + + public String foo() { + return "foo"; + } + +} diff --git a/gh-7174/src/test/java/example/BarTest.java b/gh-7174/src/test/java/example/BarTest.java index fbc2ce9..d5dbfcd 100644 --- a/gh-7174/src/test/java/example/BarTest.java +++ b/gh-7174/src/test/java/example/BarTest.java @@ -5,22 +5,28 @@ 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.context.annotation.Import; 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.*; +/** + * In this test suite I want to test only BarService. + * I don't care about FooService - the default NOOP implementation from TestConfiguration is fine + */ @RunWith(SpringRunner.class) @WebMvcTest +@Import({TestConfiguration.class}) public class BarTest { - @MockBean FooBarService service; + @MockBean BarService barService; @Autowired MockMvc mockMvc; @Test public void shouldReturnStatus500() throws Exception { - doThrow(new RuntimeException()).when(service).bar(); + doThrow(new RuntimeException()).when(barService).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 index 3b3ce20..6622e99 100644 --- a/gh-7174/src/test/java/example/FooTest.java +++ b/gh-7174/src/test/java/example/FooTest.java @@ -5,23 +5,29 @@ 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.context.annotation.Import; 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.*; +/** + * In this test suite I want to test only FooService. + * I don't care about BarService - the default NOOP implementation from TestConfiguration is fine + */ @RunWith(SpringRunner.class) @WebMvcTest +@Import({TestConfiguration.class}) public class FooTest { - @MockBean FooBarService service; + @MockBean FooService fooService; @Autowired MockMvc mockMvc; @Test public void shouldReturnXYZ() throws Exception { final String expected = "xyz"; - when(service.foo()).thenReturn(expected); + when(fooService.foo()).thenReturn(expected); mockMvc.perform(get("/foo")) .andExpect(content().string(expected)); } diff --git a/gh-7174/src/test/java/example/TestConfiguration.java b/gh-7174/src/test/java/example/TestConfiguration.java new file mode 100644 index 0000000..b5918d1 --- /dev/null +++ b/gh-7174/src/test/java/example/TestConfiguration.java @@ -0,0 +1,23 @@ +package example; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import static org.mockito.Mockito.*; + +/** + * Here we create some default implementations for beans used in WebMvc context. + */ +@Configuration +public class TestConfiguration { + + @Bean + public FooService fooService() { + return mock(FooService.class); + } + + @Bean + public BarService barService() { + return mock(BarService.class); + } + +}