使用@SpringBootTest注解進(jìn)行單元測試
概述
@SpringBootTest注解是SpringBoot自1.4.0版本開始引入的一個(gè)用于測試的注解?;居梅ㄈ缦拢?/p>
1. 添加Maven依賴
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
2. 編寫啟動入口類
@SpringBootApplication
public class StartUpApplication {
public static void main(String[] args) {
SpringApplication.run(StartUpApplication.class, args);
}
}
3. 編寫Controller類
@RestController
public class HelloController {
@RequestMapping("/")
public String index() {
return "Hello Spring Boot,Index!";
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "Spring Boot Test Demo!";
}
}
4. 編寫測試類
@RunWith(SpringRunner.class)
@SpringBootTest(classes = StartUpApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerTest {
/**
* @LocalServerPort 提供了 @Value("${local.server.port}") 的代替
*/
@LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate restTemplate;
@Before
public void setUp() throws Exception {
String url = String.format("http://localhost:%d/", port);
System.out.println(String.format("port is : [%d]", port));
this.base = new URL(url);
}
/**
* 向"/test"地址發(fā)送請求,并打印返回結(jié)果
* @throws Exception
*/
@Test
public void test1() throws Exception {
ResponseEntity<String> response = this.restTemplate.getForEntity(
this.base.toString() + "/test", String.class, "");
System.out.println(String.format("測試結(jié)果為:%s", response.getBody()));
}
其中,classes屬性指定啟動類,SpringBootTest.WebEnvironment.RANDOM_PORT經(jīng)常和測試類中@LocalServerPort一起在注入屬性時(shí)使用。會隨機(jī)生成一個(gè)端口號。
總結(jié)
我們發(fā)現(xiàn),隨著Spring boot 版本的提升,單元測試變得更簡單了。
到此這篇關(guān)于使用@SpringBootTest注解進(jìn)行單元測試的文章就介紹到這了,更多相關(guān)@SpringBootTest 單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)組實(shí)現(xiàn)Java 自定義Queue隊(duì)列及應(yīng)用操作
這篇文章主要介紹了數(shù)組實(shí)現(xiàn)Java 自定義Queue隊(duì)列及應(yīng)用操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
基于Java實(shí)現(xiàn)考試管理系統(tǒng)
這篇文章主要介紹了基于Java實(shí)現(xiàn)的考試管理系統(tǒng),項(xiàng)目運(yùn)用到的技術(shù)有Springboot、Maven、Jpa、Vue等等,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12
Java中l(wèi)ong類型與Long類型的區(qū)別和大小比較詳解
這篇文章主要給大家介紹了Java中l(wèi)ong類型與Long類型區(qū)別和大小比較的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
SpringBoot自定義FailureAnalyzer過程解析
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
MyBatis使用annonation定義類型映射的簡易用法示例
這篇文章主要介紹了MyBatis使用annonation定義類型映射的簡易用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Springboot?RestTemplate設(shè)置超時(shí)時(shí)間的簡單方法
學(xué)習(xí)springboot ,RestTemplate的使用場景非常非常多,比如springcloud中的服務(wù)消費(fèi),下面這篇文章主要給大家介紹了關(guān)于Springboot?RestTemplate設(shè)置超時(shí)時(shí)間的簡單方法,需要的朋友可以參考下2022-01-01
mybatis plus實(shí)體類中字段映射mysql中的json格式方式
這篇文章主要介紹了mybatis plus實(shí)體類中字段映射mysql中的json格式方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

