-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
tiny-facebook/src/main/java/com/b4u/tinyfacebook/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.b4u.tinyfacebook; | ||
|
||
|
||
import java.util.Arrays; | ||
|
||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* @Author: hzy | ||
* @Description: | ||
* @Date: Created on 21:34 2017/10/15 | ||
*/ | ||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class, args); | ||
} | ||
|
||
@Bean | ||
public CommandLineRunner commandLineRunner(ApplicationContext ctx) { | ||
return args -> { | ||
|
||
System.out.println("Let's inspect the beans provided by Spring Boot:"); | ||
|
||
String[] beanNames = ctx.getBeanDefinitionNames(); | ||
Arrays.sort(beanNames); | ||
for (String beanName : beanNames) { | ||
System.out.println(beanName); | ||
} | ||
|
||
}; | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
tiny-facebook/src/main/java/com/b4u/tinyfacebook/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.b4u.tinyfacebook.controller; | ||
|
||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
/** | ||
* @Author: hzy | ||
* @Description: | ||
* @Date: Created on 19:47 2017/10/16 | ||
*/ | ||
@RestController | ||
public class PostController { | ||
@RequestMapping("/post") | ||
public String post() { | ||
return "this is a post"; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.b4u.tinyfacebook.controller; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.net.URL; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.context.embedded.LocalServerPort; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
/** | ||
* @Author: hzy | ||
* @Description: | ||
* @Date: Created on 19:59 2017/10/16 | ||
*/ | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class PostControllerIT { | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
private URL base; | ||
|
||
@Autowired | ||
private TestRestTemplate template; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
this.base = new URL("http://localhost:" + port + "/"); | ||
} | ||
|
||
@Test | ||
public void getHello() throws Exception { | ||
ResponseEntity<String> response = template.getForEntity(base.toString()+"post", | ||
String.class); | ||
assertThat(response.getBody(), equalTo("this is a post")); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.b4u.tinyfacebook.controller; | ||
|
||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
|
||
/** | ||
* @Author: hzy | ||
* @Description: | ||
* @Date: Created on 19:55 2017/10/16 | ||
*/ | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
public class PostControllerTest { | ||
@Autowired | ||
private MockMvc mvc; | ||
|
||
@Test | ||
public void getPost() throws Exception { | ||
mvc.perform(MockMvcRequestBuilders.get("/post").accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andExpect(content().string(equalTo("this is a post"))); | ||
} | ||
} |