Aleena Rose-Mathew пре 6 година
родитељ
комит
c5ceb4015b

+ 2
- 1
complete/src/main/java/hello/HelloController.java Прегледај датотеку

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

+ 19
- 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>
@@ -18,6 +18,23 @@
18 18
             <groupId>org.springframework.boot</groupId>
19 19
             <artifactId>spring-boot-starter-web</artifactId>
20 20
         </dependency>
21
+        <dependency>
22
+            <groupId>io.springfox</groupId>
23
+            <artifactId>springfox-swagger-ui</artifactId>
24
+            <version>2.2.2</version>
25
+            <scope>compile</scope>
26
+        </dependency>
27
+        <dependency>
28
+            <groupId>io.springfox</groupId>
29
+            <artifactId>springfox-swagger2</artifactId>
30
+            <version>2.2.2</version>
31
+            <scope>compile</scope>
32
+        </dependency>
33
+        <dependency>
34
+            <groupId>org.springframework.boot</groupId>
35
+            <artifactId>spring-boot-starter-test</artifactId>
36
+            <scope>test</scope>
37
+        </dependency>
21 38
     </dependencies>
22 39
 
23 40
     <properties>
@@ -34,4 +51,4 @@
34 51
         </plugins>
35 52
     </build>
36 53
 
37
-</project>
54
+</project>

+ 9
- 7
initial/src/main/java/hello/HelloController.java Прегледај датотеку

@@ -1,15 +1,17 @@
1
+
1 2
 package hello;
2 3
 
4
+
5
+import org.springframework.web.bind.annotation.RequestMethod;
3 6
 import org.springframework.web.bind.annotation.RestController;
4 7
 import org.springframework.web.bind.annotation.RequestMapping;
5 8
 
6
-@RestController//meaning it's ready for use by Spring MVC to handle web requests.
9
+@RestController
7 10
 public class HelloController {
8
-    
9
-    @RequestMapping("/greeting")//to invoke index method
10
-    public String index()
11
-    {
11
+
12
+    @RequestMapping(method = RequestMethod.GET, value = "/index")
13
+    public String index() {
12 14
         return "Greetings from Spring Boot!";
13 15
     }
14
-    
15
-}
16
+
17
+}

+ 40
- 0
initial/src/test/java/hello/HelloControllorIT.java Прегледај датотеку

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

+ 42
- 0
initial/src/test/java/hello/HelloControllorTest.java Прегледај датотеку

@@ -0,0 +1,42 @@
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
+import java.net.URL;
19
+@RunWith(SpringRunner.class)
20
+@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
21
+public class HelloControllorTest {
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
+}