nafis nibir 8 лет назад
Родитель
Сommit
f20485bd9a

+ 2
- 0
initial/build.gradle Просмотреть файл

@@ -4,6 +4,7 @@ buildscript {
4 4
     }
5 5
     dependencies {
6 6
         classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.3.RELEASE")
7
+
7 8
     }
8 9
 }
9 10
 
@@ -28,5 +29,6 @@ targetCompatibility = 1.8
28 29
 dependencies {
29 30
     compile("org.springframework.boot:spring-boot-starter-web")
30 31
     testCompile("junit:junit")
32
+    testCompile("org.springframework.boot:spring-boot-starter-test")
31 33
 }
32 34
 

+ 41
- 2
initial/pom.xml Просмотреть файл

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
-    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4 4
     <modelVersion>4.0.0</modelVersion>
5 5
 
6 6
     <groupId>org.springframework</groupId>
@@ -16,8 +16,47 @@
16 16
     <dependencies>
17 17
         <dependency>
18 18
             <groupId>org.springframework.boot</groupId>
19
+            <artifactId>spring-boot-starter-actuator</artifactId>
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>
19 28
             <artifactId>spring-boot-starter-web</artifactId>
20 29
         </dependency>
30
+        <dependency>
31
+            <groupId>org.hamcrest</groupId>
32
+            <artifactId>hamcrest-library</artifactId>
33
+        </dependency>
34
+        <dependency>
35
+            <groupId>org.springframework</groupId>
36
+            <artifactId>spring-test</artifactId>
37
+            <version>RELEASE</version>
38
+            <scope>compile</scope>
39
+        </dependency>
40
+        <dependency>
41
+            <groupId>org.springframework.boot</groupId>
42
+            <artifactId>spring-boot-test</artifactId>
43
+        </dependency>
44
+        <dependency>
45
+            <groupId>junit</groupId>
46
+            <artifactId>junit</artifactId>
47
+        </dependency>
48
+        <dependency>
49
+            <groupId>org.springframework.boot</groupId>
50
+            <artifactId>spring-boot-test-autoconfigure</artifactId>
51
+        </dependency>
52
+        <dependency>
53
+            <groupId>junit</groupId>
54
+            <artifactId>junit</artifactId>
55
+        </dependency>
56
+        <dependency>
57
+            <groupId>org.junit.jupiter</groupId>
58
+            <artifactId>junit-jupiter-api</artifactId>
59
+        </dependency>
21 60
     </dependencies>
22 61
 
23 62
     <properties>
@@ -34,4 +73,4 @@
34 73
         </plugins>
35 74
     </build>
36 75
 
37
-</project>
76
+</project>

+ 1
- 0
initial/src/main/java/Main.java Просмотреть файл

@@ -0,0 +1 @@
1
+

+ 2
- 0
initial/src/main/java/hello/Application.java Просмотреть файл

@@ -2,6 +2,7 @@ package hello;
2 2
 
3 3
 import java.util.Arrays;
4 4
 
5
+import org.junit.runner.RunWith;
5 6
 import org.springframework.boot.SpringApplication;
6 7
 import org.springframework.boot.autoconfigure.SpringBootApplication;
7 8
 import org.springframework.context.ApplicationContext;
@@ -19,6 +20,7 @@ public class Application {
19 20
         for (String beanName : beanNames) {
20 21
             System.out.println(beanName);
21 22
         }
23
+        SpringApplication.run(Application.class, args);
22 24
     }
23 25
 
24 26
 }

+ 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 org.junit.Test;
4
+import org.junit.runner.RunWith;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
7
+import org.springframework.boot.test.context.SpringBootTest;
8
+import org.springframework.http.MediaType;
9
+import org.springframework.test.context.junit4.SpringRunner;
10
+import org.springframework.test.web.servlet.MockMvc;
11
+import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
12
+
13
+import static org.hamcrest.Matchers.equalTo;
14
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
15
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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 Просмотреть файл

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