JaseG256 преди 6 години
родител
ревизия
f62c393bcf

+ 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>org.springframework.boot</groupId>
23
+            <artifactId>spring-boot-starter-test</artifactId>
24
+            <scope>test</scope>
25
+        </dependency>
26
+        <dependency>
27
+            <groupId>junit</groupId>
28
+            <artifactId>junit</artifactId>
29
+        </dependency>
30
+        <dependency>
31
+            <groupId>org.springframework.boot</groupId>
32
+            <artifactId>spring-boot-test-autoconfigure</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.hamcrest</groupId>
42
+            <artifactId>hamcrest-library</artifactId>
43
+        </dependency>
44
+        <dependency>
45
+            <groupId>org.springframework.boot</groupId>
46
+            <artifactId>spring-boot-starter-actuator</artifactId>
47
+        </dependency>
21 48
     </dependencies>
22 49
 
23 50
     <properties>

+ 16
- 9
initial/src/main/java/hello/Application.java Целия файл

@@ -2,23 +2,30 @@ 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);
22 16
     }
23 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
+    }
24 31
 }

+ 2
- 5
initial/src/main/java/hello/HelloController.java Целия файл

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

+ 30
- 0
initial/src/main/java/hello/HelloControllerTest.java Целия файл

@@ -0,0 +1,30 @@
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
+@RunWith(SpringRunner.class)
17
+@SpringBootTest
18
+@AutoConfigureMockMvc
19
+public class HelloControllerTest {
20
+
21
+    @Autowired
22
+    private MockMvc mvc;
23
+
24
+    @Test
25
+    public void getHello() throws Exception {
26
+        mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
27
+                .andExpect(status().isOk())
28
+                .andExpect(content().string(equalTo("Greetings from Spring Boot!")));
29
+    }
30
+}

+ 39
- 0
initial/src/main/java/hello/HelloControllerTestTwo.java Целия файл

@@ -0,0 +1,39 @@
1
+package hello;
2
+
3
+import static org.junit.Assert.*;
4
+import static org.hamcrest.Matchers.*;
5
+import static org.junit.Assert.*;
6
+import java.net.URL;
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 HelloControllerTestTwo {
20
+
21
+    @LocalServerPort
22
+    private int port;
23
+    private URL base;
24
+
25
+    @Autowired
26
+    private TestRestTemplate template;
27
+
28
+    @Before
29
+    public void setUp() throws Exception {
30
+        this.base = new URL("http://localhost:" + port + "/");
31
+    }
32
+
33
+    @Test
34
+    public void getHello() throws Exception {
35
+        ResponseEntity<String> response = template.getForEntity(base.toString(),
36
+                String.class);
37
+        assertThat(response.getBody(), equalTo("Greetings from Spring Boot!"));
38
+    }
39
+}