Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions gh-7174/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<version>1.4.1.RELEASE</version>
</parent>

<properties>
<java.version>1.8</java.version>
<spring.version>4.3.4.BUILD-SNAPSHOT</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -25,17 +30,22 @@
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-snapshot</id>
<name>Spring Portfolio Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
</repository>
</repositories>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions gh-7174/src/main/java/example/FooBarApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,21 @@
@RestController
public class FooBarApplication {

@Autowired FooBarService service;
@Autowired FooService fooService;
@Autowired BarService barService;

public static void main(String[] args) {
SpringApplication.run(FooBarApplication.class, args);
}

@GetMapping("/foo")
public String foo() {
return service.foo();
return fooService.foo();
}

@GetMapping("/bar")
public void bar() {
service.bar();
barService.bar();
}

@ExceptionHandler(RuntimeException.class)
Expand Down
12 changes: 12 additions & 0 deletions gh-7174/src/main/java/example/FooService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package example;

import org.springframework.stereotype.Service;

@Service
public class FooService {

public String foo() {
return "foo";
}

}
10 changes: 8 additions & 2 deletions gh-7174/src/test/java/example/BarTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 8 additions & 2 deletions gh-7174/src/test/java/example/FooTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
23 changes: 23 additions & 0 deletions gh-7174/src/test/java/example/TestConfiguration.java
Original file line number Diff line number Diff line change
@@ -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);
}

}