Spring Boot提供如下方式進(jìn)行測試Controller
- 直接注入Controller
- 基于一個mock的MVC,沒有啟動Server
- 基于客戶端赏殃,啟動Server
maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
定義一個Controller
@Controller
public class HomeController {
@RequestMapping("/")
public @ResponseBody
String greeting() {
return "Hello, World";
}
}
測試Controller
package org.ysy.study.bcdb;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.ysy.study.bcdb.controller.HomeController;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
class BcdbApplicationTests {
@Autowired
private HomeController controller;
@Test
void contextLoads() {
assertThat(controller).isNotNull();
}
}
測試基于MVCMock
package org.ysy.study.bcdb;
import org.junit.jupiter.api.Test;
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.test.web.servlet.MockMvc;
import static org.hamcrest.core.StringContains.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Desc: TestingWebApplicationTest
* Created ysy on 2020/8/20 19:01.
*/
@SpringBootTest
@AutoConfigureMockMvc
public class TestingWebApplicationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello, World")));
}
}
測試基于啟動tomcat服務(wù)
package org.ysy.study.bcdb;
import org.junit.jupiter.api.Test;
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.boot.web.server.LocalServerPort;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Desc: HttpRequestTest
* Created ysy on 2020/8/20 18:57.
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Test
public void greetingShouldReturnDefaultMessage() throws Exception {
System.out.println(port);
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/bcdb/",
String.class)).contains("Hello, World");
}
}