From cc42fa626eeeab113207b8b94db4651f4d1d9521 Mon Sep 17 00:00:00 2001 From: hzy38324 <2668612407@qq.com> Date: Mon, 16 Oct 2017 20:39:12 +0800 Subject: [PATCH] get started --- .../com/b4u/tinyfacebook/Application.java | 39 ++++++++++++++++ .../controller/PostController.java | 17 +++++++ .../controller/PostControllerIT.java | 46 +++++++++++++++++++ .../controller/PostControllerTest.java | 36 +++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 tiny-facebook/src/main/java/com/b4u/tinyfacebook/Application.java create mode 100644 tiny-facebook/src/main/java/com/b4u/tinyfacebook/controller/PostController.java create mode 100644 tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerIT.java create mode 100644 tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerTest.java diff --git a/tiny-facebook/src/main/java/com/b4u/tinyfacebook/Application.java b/tiny-facebook/src/main/java/com/b4u/tinyfacebook/Application.java new file mode 100644 index 0000000..8a3f6d8 --- /dev/null +++ b/tiny-facebook/src/main/java/com/b4u/tinyfacebook/Application.java @@ -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); + } + + }; + } + +} \ No newline at end of file diff --git a/tiny-facebook/src/main/java/com/b4u/tinyfacebook/controller/PostController.java b/tiny-facebook/src/main/java/com/b4u/tinyfacebook/controller/PostController.java new file mode 100644 index 0000000..dd5531c --- /dev/null +++ b/tiny-facebook/src/main/java/com/b4u/tinyfacebook/controller/PostController.java @@ -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"; + } +} diff --git a/tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerIT.java b/tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerIT.java new file mode 100644 index 0000000..39014ad --- /dev/null +++ b/tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerIT.java @@ -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 response = template.getForEntity(base.toString()+"post", + String.class); + assertThat(response.getBody(), equalTo("this is a post")); + } +} \ No newline at end of file diff --git a/tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerTest.java b/tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerTest.java new file mode 100644 index 0000000..1ab749e --- /dev/null +++ b/tiny-facebook/src/test/java/com/b4u/tinyfacebook/controller/PostControllerTest.java @@ -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"))); + } +}