mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Merge pull request #73 from strongant/master
完善SpringBoot Hello World 单元测试
This commit is contained in:
@@ -0,0 +1 @@
|
||||
server.port=-1
|
||||
@@ -1,16 +1,38 @@
|
||||
package demo.springboot;
|
||||
|
||||
|
||||
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.TestPropertySource;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = QuickStartApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
@TestPropertySource(locations = "classpath:application.properties")
|
||||
public class QuickStartApplicationTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mvc;
|
||||
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
public void requestHello_thenStatus200_and_outputHello() throws Exception {
|
||||
mvc.perform(get("/hello")
|
||||
.contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().encoding("UTF-8"))
|
||||
.andExpect(content().string("Hello,Spring Boot!"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
server.port=-1
|
||||
Reference in New Issue
Block a user