#11 SpringOne_mexiliang

開啟中
mexiliang 請求將 1 次程式碼提交從 mexiliang/ZCW-SpringOne:master 合併至 master

+ 13
- 0
initial/app.groovy 查看文件

@@ -0,0 +1,13 @@
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
+
10
+        return "Hello World!"
11
+    }
12
+
13
+}

+ 9
- 0
initial/pom.xml 查看文件

@@ -18,6 +18,15 @@
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>org.springframework.boot</groupId>
28
+            <artifactId>spring-boot-starter-actuator</artifactId>
29
+        </dependency>
21 30
     </dependencies>
22 31
 
23 32
     <properties>

+ 19
- 10
initial/src/main/java/hello/Application.java 查看文件

@@ -2,23 +2,32 @@ package hello;
2 2
 
3 3
 import java.util.Arrays;
4 4
 
5
+import org.springframework.boot.CommandLineRunner;
5 6
 import org.springframework.boot.SpringApplication;
6 7
 import org.springframework.boot.autoconfigure.SpringBootApplication;
7 8
 import org.springframework.context.ApplicationContext;
9
+import org.springframework.context.annotation.Bean;
8 10
 
9 11
 @SpringBootApplication
10 12
 public class Application {
11
-    
13
+
12 14
     public static void main(String[] args) {
13 15
         ApplicationContext ctx = SpringApplication.run(Application.class, args);
14
-        
15
-        System.out.println("Let's inspect the beans provided by Spring Boot:");
16
-        
17
-        String[] beanNames = ctx.getBeanDefinitionNames();
18
-        Arrays.sort(beanNames);
19
-        for (String beanName : beanNames) {
20
-            System.out.println(beanName);
21
-        }
16
+
22 17
     }
23 18
 
24
-}
19
+    @Bean
20
+    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
21
+        return args -> {
22
+            System.out.println("Let's inspect the beans provided by Spring Boot:");
23
+
24
+            String[] beanNames = ctx.getBeanDefinitionNames();
25
+            Arrays.sort(beanNames);
26
+            for (String beanName : beanNames) {
27
+                System.out.println(beanName);
28
+            }
29
+        };
30
+
31
+
32
+    }
33
+}

+ 1
- 0
initial/src/main/java/hello/HelloController.java 查看文件

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

+ 44
- 0
initial/src/test/hello/HelloControllerIT.java 查看文件

@@ -0,0 +1,44 @@
1
+package hello;
2
+import static org.hamcrest.Matchers.*;
3
+import static org.junit.Assert.*;
4
+
5
+import java.net.URL;
6
+
7
+import org.junit.Before;
8
+import org.junit.Test;
9
+import org.junit.runner.RunWith;
10
+import org.springframework.beans.factory.annotation.Autowired;
11
+import org.springframework.boot.test.context.SpringBootTest;
12
+import org.springframework.boot.test.web.client.TestRestTemplate;
13
+import org.springframework.boot.web.server.LocalServerPort;
14
+import org.springframework.http.ResponseEntity;
15
+import org.springframework.test.context.junit4.SpringRunner;
16
+
17
+@RunWith(SpringRunner.class)
18
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
19
+public class HelloControllerIT {
20
+
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
+    }
42
+
43
+
44
+

+ 31
- 0
initial/src/test/hello/HelloControllerTest.java 查看文件

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