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
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);
+ }
+
+}