首先maven要引入spring-boot-starter-test
這個包。
先看一段程式碼
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) public class MyTest { @Autowired private TestRestTemplate restTemplate; @Test public void test() { this.restTemplate.getForEntity( "/{username}/vehicle", String.class, "Phil"); } }
1、@RunWith(SpringRunner.class) 告訴JUnit執行使用Spring的測試支援。SpringRunner是SpringJUnit4ClassRunner的新名字,這個名字只是讓名字看起來簡單些。
2、@SpringBootTest的意思是“帶有Spring Boot支援的載入程式”(例如,載入應用程式、屬性,為我們提供Spring Boot的所有精華部分)。
3、webEnvironment屬性允許為測試配置特定的“網路環境”。你可以利用一個MOCK小服務程式環境開始你的測試,或者使用一個執行在RANDOM_PORT 或者 DEFINED_PORT上的真正的HTTP伺服器。
4、如果我們想要載入一個特定的配置,我們可以用@SpringBootTest class屬性。在這個例項中,我們省略classes就意味著測試要首次嘗試從任意一個inner-classes中載入@ configuration,如果這個嘗試失敗了,它會在你主要的@SpringBootApplicationclass中進行搜尋。
------------------------------------------------------------------------------------------------------------------------------
其中IndexController是你寫的controller
@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class IndexTest { private MockMvc mvc; @Autowired private IndexController indexController; @Before public void setup() { this.mvc = MockMvcBuilders.standaloneSetup(indexController).build(); } @Test public void t() throws Exception { assertNotNull(mvc); mvc.perform(MockMvcRequestBuilders.get("/h"). accept(MediaType.parseMediaType("application/json;charset=UTF-8"))) .andExpect(MockMvcResultMatchers.status().isOk()); } }
這個是第二種測試方法:
package com.hexun.bdc.auth.controller; import com.hexun.bdc.auth.client.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertNotNull; /** * Created by iceblue on 2017/3/26.webEnvironment=RANDOM_PORT
表示啟動服務時埠隨機(避免埠衝突) */ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class) @AutoConfigureMockMvc public class IndexControllerTest { @Autowired protected MockMvc mvc; @LocalServerPort // 注入埠 private Integer port; @Autowired private TestRestTemplate restTemplate; private static final Logger logger = LoggerFactory.getLogger(IndexControllerTest.class); @Test public void t() throws Exception { assertNotNull(mvc); logger.info("# port:" + port.toString()); assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/h", String.class)).contains("hello"); // mvc.perform(MockMvcRequestBuilders.get("/h")) // .andExpect(MockMvcResultMatchers.status().isOk()); } }
是第三種測試方法,直接利用TestRestTemplate類
package com.hexun.bdc.auth.controller; import com.hexun.bdc.auth.client.Application; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = Application.class) public class IndexTest2 { @Autowired private TestRestTemplate restTemplate; private static final Logger logger = LoggerFactory.getLogger(IndexTest2.class); @Test public void t() throws Exception { assertNotNull(restTemplate); String body = this.restTemplate.getForObject("/h", String.class); System.out.println("body = " + body); logger.info("body = " + body); } }