Skip to content

Commit

Permalink
get started
Browse files Browse the repository at this point in the history
  • Loading branch information
hzy38324 committed Oct 16, 2017
1 parent f60adf8 commit cc42fa6
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tiny-facebook/src/main/java/com/b4u/tinyfacebook/Application.java
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);
}

};
}

}
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";
}
}
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"));
}
}
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")));
}
}

0 comments on commit cc42fa6

Please sign in to comment.