Просмотр исходного кода

Added a basic controller unit test and integration test

Fixes gh-18
Ian Gilham 10 лет назад
Родитель
Сommit
6d98b87891

+ 1
- 1
complete/build.gradle Просмотреть файл

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

+ 18
- 0
complete/pom.xml Просмотреть файл

@@ -24,9 +24,16 @@
24 24
             <artifactId>spring-boot-starter-actuator</artifactId>
25 25
         </dependency>
26 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 32
     </dependencies>
28 33
 
29 34
     <properties>
35
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
36
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
30 37
         <start-class>hello.Application</start-class>
31 38
     </properties>
32 39
 
@@ -36,6 +43,17 @@
36 43
                 <groupId>org.springframework.boot</groupId>
37 44
                 <artifactId>spring-boot-maven-plugin</artifactId>
38 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 57
         </plugins>
40 58
     </build>
41 59
 

+ 1
- 1
complete/src/main/java/hello/HelloController.java Просмотреть файл

@@ -6,7 +6,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
6 6
 @RestController
7 7
 public class HelloController {
8 8
     
9
-    @RequestMapping("/")
9
+    @RequestMapping("/hello")
10 10
     public String index() {
11 11
         return "Greetings from Spring Boot!";
12 12
     }

+ 38
- 0
complete/src/test/java/hello/HelloControllerIT.java Просмотреть файл

@@ -0,0 +1,38 @@
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 Просмотреть файл

@@ -0,0 +1,38 @@
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
+}