使用@SpringBootTest注解進行單元測試
概述
@SpringBootTest注解是SpringBoot自1.4.0版本開始引入的一個用于測試的注解。基本用法如下:
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一起在注入屬性時使用。會隨機生成一個端口號。
總結(jié)
我們發(fā)現(xiàn),隨著Spring boot 版本的提升,單元測試變得更簡單了。
到此這篇關(guān)于使用@SpringBootTest注解進行單元測試的文章就介紹到這了,更多相關(guān)@SpringBootTest 單元測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)組實現(xiàn)Java 自定義Queue隊列及應(yīng)用操作
這篇文章主要介紹了數(shù)組實現(xiàn)Java 自定義Queue隊列及應(yīng)用操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06Java中l(wèi)ong類型與Long類型的區(qū)別和大小比較詳解
這篇文章主要給大家介紹了Java中l(wèi)ong類型與Long類型區(qū)別和大小比較的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11SpringBoot自定義FailureAnalyzer過程解析
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11MyBatis使用annonation定義類型映射的簡易用法示例
這篇文章主要介紹了MyBatis使用annonation定義類型映射的簡易用法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-09-09Springboot?RestTemplate設(shè)置超時時間的簡單方法
學(xué)習(xí)springboot ,RestTemplate的使用場景非常非常多,比如springcloud中的服務(wù)消費,下面這篇文章主要給大家介紹了關(guān)于Springboot?RestTemplate設(shè)置超時時間的簡單方法,需要的朋友可以參考下2022-01-01mybatis plus實體類中字段映射mysql中的json格式方式
這篇文章主要介紹了mybatis plus實體類中字段映射mysql中的json格式方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08