完善SpringBoot Hello World 单元测试

This commit is contained in:
bwh
2019-04-11 00:04:07 +08:00
parent ea8b763cc1
commit cd6fa32da8
3 changed files with 28 additions and 4 deletions

View File

@@ -1,16 +1,38 @@
package demo.springboot; package demo.springboot;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; 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.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.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) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = QuickStartApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application.properties")
public class QuickStartApplicationTests { public class QuickStartApplicationTests {
@Test @Autowired
public void contextLoads() { private MockMvc mvc;
}
@Test
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("HelloSpring Boot"));
}
} }

View File

@@ -0,0 +1 @@
server.port=-1