Ahmad Rusdi 6 年前
父节点
当前提交
89a4d13097

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

@@ -0,0 +1,9 @@
1
+@RestController
2
+class ThisWillActuallyRun {
3
+
4
+    @RequestMapping("/")
5
+    String home() {
6
+        return "Hello World!"
7
+    }
8
+
9
+}

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

@@ -18,6 +18,33 @@
18 18
             <groupId>org.springframework.boot</groupId>
19 19
             <artifactId>spring-boot-starter-web</artifactId>
20 20
         </dependency>
21
+        <dependency>
22
+            <groupId>junit</groupId>
23
+            <artifactId>junit</artifactId>
24
+        </dependency>
25
+        <dependency>
26
+            <groupId>org.springframework.boot</groupId>
27
+            <artifactId>spring-boot-starter-test</artifactId>
28
+            <scope>test</scope>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>org.springframework.boot</groupId>
32
+            <artifactId>spring-boot-starter-actuator</artifactId>
33
+        </dependency>
34
+        <dependency>
35
+            <groupId>org.springframework</groupId>
36
+            <artifactId>spring-test</artifactId>
37
+            <version>5.0.7.RELEASE</version>
38
+            <scope>compile</scope>
39
+        </dependency>
40
+        <dependency>
41
+            <groupId>org.springframework.boot</groupId>
42
+            <artifactId>spring-boot-test-autoconfigure</artifactId>
43
+        </dependency>
44
+        <dependency>
45
+            <groupId>org.hamcrest</groupId>
46
+            <artifactId>hamcrest-library</artifactId>
47
+        </dependency>
21 48
     </dependencies>
22 49
 
23 50
     <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
-        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
-        }
15
+        SpringApplication.run(Application.class, args);
16
+    }
17
+
18
+    @Bean
19
+    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
20
+        return args -> {
21
+
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
+        };
22 31
     }
23 32
 
24 33
 }

+ 42
- 0
initial/src/main/java/hello/HelloControllerIT.java 查看文件

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

+ 32
- 0
initial/src/main/java/hello/HelloControllerTest.java 查看文件

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