Chad 8 lat temu
rodzic
commit
18f25cd422

+ 33
- 0
initial/pom.xml Wyświetl plik

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
+
22
+        <dependency>
23
+            <groupId>org.springframework.boot</groupId>
24
+            <artifactId>spring-boot-starter-test</artifactId>
25
+            <scope>test</scope>
26
+        </dependency>
27
+        <dependency>
28
+            <groupId>org.springframework.boot</groupId>
29
+            <artifactId>spring-boot-test</artifactId>
30
+        </dependency>
31
+        <dependency>
32
+            <groupId>org.springframework.boot</groupId>
33
+            <artifactId>spring-boot-test-autoconfigure</artifactId>
34
+        </dependency>
35
+        <dependency>
36
+            <groupId>junit</groupId>
37
+            <artifactId>junit</artifactId>
38
+        </dependency>
39
+        <dependency>
40
+            <groupId>org.springframework</groupId>
41
+            <artifactId>spring-test</artifactId>
42
+            <version>RELEASE</version>
43
+            <scope>compile</scope>
44
+        </dependency>
45
+        <dependency>
46
+            <groupId>org.hamcrest</groupId>
47
+            <artifactId>hamcrest-library</artifactId>
48
+        </dependency>
49
+        <dependency>
50
+            <groupId>org.springframework.boot</groupId>
51
+            <artifactId>spring-boot-starter-actuator</artifactId>
52
+        </dependency>
53
+
21
     </dependencies>
54
     </dependencies>
22
 
55
 
23
     <properties>
56
     <properties>

+ 41
- 0
initial/src/main/java/hello/HelloControllerIT.java Wyświetl plik

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

+ 31
- 0
initial/src/main/java/hello/HelloControllerTest.java Wyświetl plik

1
+package hello;
2
+
3
+import static org.hamcrest.Matchers.equalTo;
4
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
5
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
6
+
7
+import org.junit.Test;
8
+import org.junit.runner.RunWith;
9
+import org.springframework.beans.factory.annotation.Autowired;
10
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11
+import org.springframework.boot.test.context.SpringBootTest;
12
+import org.springframework.http.MediaType;
13
+import org.springframework.test.context.junit4.SpringRunner;
14
+import org.springframework.test.web.servlet.MockMvc;
15
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
16
+
17
+@RunWith(SpringRunner.class)
18
+@SpringBootTest
19
+@AutoConfigureMockMvc
20
+public class HelloControllerTest {
21
+
22
+    @Autowired
23
+    private MockMvc mvc;
24
+
25
+    @Test
26
+    public void getHello() throws Exception {
27
+        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
28
+                .andExpect(status().isOk())
29
+                .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
30
+    }
31
+}

+ 12
- 0
initial/src/main/java/hello/app.groovy Wyświetl plik

1
+import org.springframework.web.bind.annotation.RequestMapping
2
+import org.springframework.web.bind.annotation.RestController
3
+
4
+@RestController
5
+class ThisWillActuallyRun {
6
+
7
+    @RequestMapping("/")
8
+    String home() {
9
+        return "Hello World!"
10
+    }
11
+
12
+}