alizalang 8 년 전
부모
커밋
744f4c1dda

+ 2
- 2
complete/src/main/java/hello/HelloController.java 파일 보기

@@ -10,5 +10,5 @@ public class HelloController {
10 10
     public String index() {
11 11
         return "Greetings from Spring Boot!";
12 12
     }
13
-    
14
-}
13
+
14
+}

+ 14
- 0
initial/app.groovy 파일 보기

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

+ 39
- 1
initial/pom.xml 파일 보기

@@ -12,12 +12,50 @@
12 12
         <artifactId>spring-boot-starter-parent</artifactId>
13 13
         <version>2.0.3.RELEASE</version>
14 14
     </parent>
15
-
16 15
     <dependencies>
17 16
         <dependency>
18 17
             <groupId>org.springframework.boot</groupId>
19 18
             <artifactId>spring-boot-starter-web</artifactId>
20 19
         </dependency>
20
+        <dependency>
21
+            <groupId>org.springframework.boot</groupId>
22
+            <artifactId>spring-boot-starter-test</artifactId>
23
+            <scope>test</scope>
24
+        </dependency>
25
+        <dependency>
26
+            <groupId>org.springframework.boot</groupId>
27
+            <artifactId>spring-boot-starter-actuator</artifactId>
28
+        </dependency>
29
+        <dependency>
30
+            <groupId>org.hamcrest</groupId>
31
+            <artifactId>hamcrest-library</artifactId>
32
+        </dependency>
33
+        <dependency>
34
+            <groupId>junit</groupId>
35
+            <artifactId>junit</artifactId>
36
+        </dependency>
37
+        <dependency>
38
+            <groupId>junit</groupId>
39
+            <artifactId>junit</artifactId>
40
+        </dependency>
41
+        <dependency>
42
+            <groupId>org.springframework.boot</groupId>
43
+            <artifactId>spring-boot-test</artifactId>
44
+        </dependency>
45
+        <dependency>
46
+            <groupId>org.springframework.boot</groupId>
47
+            <artifactId>spring-boot-test</artifactId>
48
+        </dependency>
49
+        <dependency>
50
+            <groupId>org.springframework</groupId>
51
+            <artifactId>spring-test</artifactId>
52
+            <version>RELEASE</version>
53
+            <scope>compile</scope>
54
+        </dependency>
55
+        <dependency>
56
+            <groupId>org.springframework.boot</groupId>
57
+            <artifactId>spring-boot-test-autoconfigure</artifactId>
58
+        </dependency>
21 59
     </dependencies>
22 60
 
23 61
     <properties>

+ 20
- 11
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
+}

+ 3
- 3
initial/src/main/java/hello/HelloController.java 파일 보기

@@ -5,10 +5,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
5 5
 
6 6
 @RestController
7 7
 public class HelloController {
8
-    
8
+
9 9
     @RequestMapping("/")
10 10
     public String index() {
11 11
         return "Greetings from Spring Boot!";
12 12
     }
13
-    
14
-}
13
+
14
+}

+ 41
- 0
initial/src/main/java/hello/HelloControllerIT.java 파일 보기

@@ -0,0 +1,41 @@
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 파일 보기

@@ -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
+}