Browse Source

Added a basic controller unit test and integration test

Fixes gh-18
Ian Gilham 10 years ago
parent
commit
6d98b87891

+ 1
- 1
complete/build.gradle View File

26
     // tag::actuator[]
26
     // tag::actuator[]
27
     compile("org.springframework.boot:spring-boot-starter-actuator")
27
     compile("org.springframework.boot:spring-boot-starter-actuator")
28
     // end::actuator[]
28
     // end::actuator[]
29
-    testCompile("junit:junit")
29
+    testCompile("org.springframework.boot:spring-boot-starter-test")
30
 }
30
 }
31
 
31
 
32
 task wrapper(type: Wrapper) {
32
 task wrapper(type: Wrapper) {

+ 18
- 0
complete/pom.xml View File

24
             <artifactId>spring-boot-starter-actuator</artifactId>
24
             <artifactId>spring-boot-starter-actuator</artifactId>
25
         </dependency>
25
         </dependency>
26
         <!-- end::actuator[] -->
26
         <!-- end::actuator[] -->
27
+        <dependency>
28
+            <groupId>org.springframework.boot</groupId>
29
+            <artifactId>spring-boot-starter-test</artifactId>
30
+            <scope>test</scope>
31
+        </dependency>
27
     </dependencies>
32
     </dependencies>
28
 
33
 
29
     <properties>
34
     <properties>
35
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
30
         <start-class>hello.Application</start-class>
37
         <start-class>hello.Application</start-class>
31
     </properties>
38
     </properties>
32
 
39
 
36
                 <groupId>org.springframework.boot</groupId>
43
                 <groupId>org.springframework.boot</groupId>
37
                 <artifactId>spring-boot-maven-plugin</artifactId>
44
                 <artifactId>spring-boot-maven-plugin</artifactId>
38
             </plugin>
45
             </plugin>
46
+            <plugin>
47
+                <artifactId>maven-failsafe-plugin</artifactId>
48
+                <executions>
49
+                    <execution>
50
+                        <goals>
51
+                            <goal>integration-test</goal>
52
+                            <goal>verify</goal>
53
+                        </goals>
54
+                    </execution>
55
+                </executions>
56
+            </plugin>
39
         </plugins>
57
         </plugins>
40
     </build>
58
     </build>
41
 
59
 

+ 1
- 1
complete/src/main/java/hello/HelloController.java View File

6
 @RestController
6
 @RestController
7
 public class HelloController {
7
 public class HelloController {
8
     
8
     
9
-    @RequestMapping("/")
9
+    @RequestMapping("/hello")
10
     public String index() {
10
     public String index() {
11
         return "Greetings from Spring Boot!";
11
         return "Greetings from Spring Boot!";
12
     }
12
     }

+ 38
- 0
complete/src/test/java/hello/HelloControllerIT.java View File

1
+package hello;
2
+
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+import org.junit.runner.RunWith;
6
+import org.springframework.boot.test.IntegrationTest;
7
+import org.springframework.boot.test.SpringApplicationConfiguration;
8
+import org.springframework.boot.test.TestRestTemplate;
9
+import org.springframework.http.ResponseEntity;
10
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
+import org.springframework.test.context.web.WebAppConfiguration;
12
+import org.springframework.web.client.RestTemplate;
13
+
14
+import java.net.URL;
15
+
16
+import static org.hamcrest.Matchers.is;
17
+import static org.junit.Assert.assertThat;
18
+
19
+@RunWith(SpringJUnit4ClassRunner.class)
20
+@SpringApplicationConfiguration(classes = Application.class)
21
+@WebAppConfiguration
22
+@IntegrationTest({"server.port=9090"})
23
+public class HelloControllerIT {
24
+	private URL base;
25
+	private RestTemplate template;
26
+
27
+	@Before
28
+	public void setUp() throws Exception {
29
+		this.base = new URL("http://localhost:9090/hello");
30
+		template = new TestRestTemplate();
31
+	}
32
+
33
+	@Test
34
+	public void getHello() throws Exception {
35
+		ResponseEntity<String> response = template.getForEntity(base.toString(), String.class);
36
+		assertThat(response.getBody(), is("Greetings from Spring Boot!"));
37
+	}
38
+}

+ 38
- 0
complete/src/test/java/hello/HelloControllerTest.java View File

1
+package hello;
2
+
3
+import org.junit.Before;
4
+import org.junit.Test;
5
+import org.junit.runner.RunWith;
6
+import org.springframework.boot.test.SpringApplicationConfiguration;
7
+import org.springframework.http.MediaType;
8
+import org.springframework.mock.web.MockServletContext;
9
+import org.springframework.test.context.ContextConfiguration;
10
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11
+import org.springframework.test.context.web.WebAppConfiguration;
12
+import org.springframework.test.web.servlet.MockMvc;
13
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
14
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
15
+
16
+import static org.hamcrest.Matchers.is;
17
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
18
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
19
+
20
+@RunWith(SpringJUnit4ClassRunner.class)
21
+@SpringApplicationConfiguration(classes = Application.class)
22
+@ContextConfiguration(classes = MockServletContext.class)
23
+@WebAppConfiguration
24
+public class HelloControllerTest {
25
+	private MockMvc mvc;
26
+
27
+	@Before
28
+	public void setUp() throws Exception {
29
+		mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
30
+	}
31
+
32
+	@Test
33
+	public void getHello() throws Exception {
34
+		mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON))
35
+				.andExpect(status().isOk())
36
+				.andExpect(content().string(is("Greetings from Spring Boot!")));
37
+	}
38
+}