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
41 changes: 41 additions & 0 deletions gh-7174/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.example</groupId>
<artifactId>spring-boot-test-context-cache</artifactId>
<version>1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.1.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>

</project>
35 changes: 35 additions & 0 deletions gh-7174/src/main/java/example/FooBarApplication.java
Original file line number Diff line number Diff line change
@@ -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
}

}
16 changes: 16 additions & 0 deletions gh-7174/src/main/java/example/FooBarService.java
Original file line number Diff line number Diff line change
@@ -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
}

}
13 changes: 13 additions & 0 deletions gh-7174/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<configuration>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="WARN">
<appender-ref ref="STDOUT" />
</root>

</configuration>
29 changes: 29 additions & 0 deletions gh-7174/src/test/java/example/BarTest.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
29 changes: 29 additions & 0 deletions gh-7174/src/test/java/example/FooTest.java
Original file line number Diff line number Diff line change
@@ -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));
}

}