Преглед на файлове

Replace curl-based test with Spring Mock MVC

Greg Turnquist преди 8 години
родител
ревизия
a042ec14e8
No account linked to committer's email
променени са 7 файла, в които са добавени 93 реда и са изтрити 21 реда
  1. 1
    1
      complete/build.gradle
  2. 10
    0
      complete/pom.xml
  3. 71
    0
      complete/src/test/java/hello/GreetingControllerTests.java
  4. 1
    1
      initial/build.gradle
  5. 10
    0
      initial/pom.xml
  6. 0
    1
      test/expected.json
  7. 0
    18
      test/run.sh

+ 1
- 1
complete/build.gradle Целия файл

@@ -26,7 +26,7 @@ targetCompatibility = 1.8
26 26
 
27 27
 dependencies {
28 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 32
 task wrapper(type: Wrapper) {

+ 10
- 0
complete/pom.xml Целия файл

@@ -18,6 +18,16 @@
18 18
             <groupId>org.springframework.boot</groupId>
19 19
             <artifactId>spring-boot-starter-web</artifactId>
20 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 31
     </dependencies>
22 32
 
23 33
     <properties>

+ 71
- 0
complete/src/test/java/hello/GreetingControllerTests.java Целия файл

@@ -0,0 +1,71 @@
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 Целия файл

@@ -26,7 +26,7 @@ targetCompatibility = 1.8
26 26
 
27 27
 dependencies {
28 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 32
 task wrapper(type: Wrapper) {

+ 10
- 0
initial/pom.xml Целия файл

@@ -18,6 +18,16 @@
18 18
             <groupId>org.springframework.boot</groupId>
19 19
             <artifactId>spring-boot-starter-web</artifactId>
20 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 31
     </dependencies>
22 32
 
23 33
     <properties>

+ 0
- 1
test/expected.json Целия файл

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

+ 0
- 18
test/run.sh Целия файл

@@ -17,24 +17,6 @@ rm -rf build
17 17
 
18 18
 cd ../complete
19 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 21
 if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; 
40 22
   then