Browse Source

Replace curl-based test with Spring Mock MVC

Greg Turnquist 8 years ago
parent
commit
a042ec14e8
No account linked to committer's email

+ 1
- 1
complete/build.gradle View File

26
 
26
 
27
 dependencies {
27
 dependencies {
28
     compile("org.springframework.boot:spring-boot-starter-web")
28
     compile("org.springframework.boot:spring-boot-starter-web")
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) {

+ 10
- 0
complete/pom.xml View File

18
             <groupId>org.springframework.boot</groupId>
18
             <groupId>org.springframework.boot</groupId>
19
             <artifactId>spring-boot-starter-web</artifactId>
19
             <artifactId>spring-boot-starter-web</artifactId>
20
         </dependency>
20
         </dependency>
21
+        <dependency>
22
+            <groupId>org.springframework.boot</groupId>
23
+            <artifactId>spring-boot-starter-test</artifactId>
24
+            <scope>test</scope>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>com.jayway.jsonpath</groupId>
28
+            <artifactId>json-path</artifactId>
29
+            <scope>test</scope>
30
+        </dependency>
21
     </dependencies>
31
     </dependencies>
22
 
32
 
23
     <properties>
33
     <properties>

+ 71
- 0
complete/src/test/java/hello/GreetingControllerTests.java View File

1
+/*
2
+ * Copyright 2016 the original author or authors.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ *      http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+package hello;
17
+
18
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
19
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
20
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
21
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
22
+
23
+import org.junit.Before;
24
+import org.junit.Test;
25
+import org.junit.runner.RunWith;
26
+
27
+import org.springframework.beans.factory.annotation.Autowired;
28
+import org.springframework.boot.test.SpringApplicationConfiguration;
29
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30
+import org.springframework.test.context.web.WebAppConfiguration;
31
+import org.springframework.test.web.servlet.MockMvc;
32
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
33
+import org.springframework.web.context.WebApplicationContext;
34
+
35
+/**
36
+ * @author Greg Turnquist
37
+ */
38
+@RunWith(SpringJUnit4ClassRunner.class)
39
+@SpringApplicationConfiguration(classes = Application.class)
40
+@WebAppConfiguration
41
+public class GreetingControllerTests {
42
+
43
+	@Autowired
44
+	private WebApplicationContext ctx;
45
+
46
+	private MockMvc mockMvc;
47
+
48
+	@Before
49
+	public void setUp() {
50
+		this.mockMvc = MockMvcBuilders.webAppContextSetup(ctx).build();
51
+	}
52
+
53
+	@Test
54
+	public void noParamGreetingShouldReturnDefaultMessage() throws Exception {
55
+
56
+		this.mockMvc.perform(get("/greeting"))
57
+				.andDo(print())
58
+				.andExpect(status().isOk())
59
+				.andExpect(jsonPath("$.content").value("Hello, World!"));
60
+	}
61
+
62
+	@Test
63
+	public void paramGreetingShouldReturnTailoredMessage() throws Exception {
64
+
65
+		this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
66
+				.andDo(print())
67
+				.andExpect(status().isOk())
68
+				.andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
69
+	}
70
+
71
+}

+ 1
- 1
initial/build.gradle View File

26
 
26
 
27
 dependencies {
27
 dependencies {
28
     compile("org.springframework.boot:spring-boot-starter-web")
28
     compile("org.springframework.boot:spring-boot-starter-web")
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) {

+ 10
- 0
initial/pom.xml View File

18
             <groupId>org.springframework.boot</groupId>
18
             <groupId>org.springframework.boot</groupId>
19
             <artifactId>spring-boot-starter-web</artifactId>
19
             <artifactId>spring-boot-starter-web</artifactId>
20
         </dependency>
20
         </dependency>
21
+        <dependency>
22
+            <groupId>org.springframework.boot</groupId>
23
+            <artifactId>spring-boot-starter-test</artifactId>
24
+            <scope>test</scope>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>com.jayway.jsonpath</groupId>
28
+            <artifactId>json-path</artifactId>
29
+            <scope>test</scope>
30
+        </dependency>
21
     </dependencies>
31
     </dependencies>
22
 
32
 
23
     <properties>
33
     <properties>

+ 0
- 1
test/expected.json View File

1
-{"id":1,"content":"Hello, World!"}

+ 0
- 18
test/run.sh View File

17
 
17
 
18
 cd ../complete
18
 cd ../complete
19
 mvn clean package
19
 mvn clean package
20
-java -jar target/gs-rest-service-0.1.0.jar &
21
-PID=$!
22
-sleep 10
23
-curl -s http://localhost:8080/greeting > target/actual.json
24
-kill -9 $PID
25
-
26
-echo "Let's look at the actual results: `cat target/actual.json`"
27
-echo "And compare it to: `cat ../test/expected.json`"
28
-
29
-if diff -w ../test/expected.json target/actual.json
30
-    then
31
-        echo SUCCESS
32
-        let ret=0
33
-    else
34
-        echo FAIL
35
-        let ret=255
36
-        exit $ret
37
-fi
38
 
20
 
39
 if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; 
21
 if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; 
40
   then 
22
   then