国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 使用@SpringBootTest注解進行單元測試

使用@SpringBootTest注解進行單元測試

2020-09-16 14:11快樂檸檬 JAVA教程

這篇文章主要介紹了使用@SpringBootTest注解進行單元測試,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

概述

@SpringBootTest注解是SpringBoot自1.4.0版本開始引入的一個用于測試的注解。基本用法如下:

1. 添加Maven依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<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. 編寫啟動入口類

?
1
2
3
4
5
6
@SpringBootApplication
public class StartUpApplication {
 public static void main(String[] args) {
  SpringApplication.run(StartUpApplication.class, args);
 }
}

3. 編寫Controller類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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. 編寫測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
@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"地址發送請求,并打印返回結果
  * @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("測試結果為:%s", response.getBody()));
 }

其中,classes屬性指定啟動類,SpringBootTest.WebEnvironment.RANDOM_PORT經常和測試類中@LocalServerPort一起在注入屬性時使用。會隨機生成一個端口號。

總結

我們發現,隨著Spring boot 版本的提升,單元測試變得更簡單了。

到此這篇關于使用@SpringBootTest注解進行單元測試的文章就介紹到這了,更多相關@SpringBootTest 單元測試內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/limenghua9112/article/details/79694849

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人的天堂在线视频 | 亚洲免费电影一区 | 在线免费黄色网址 | 婷婷精品 | 涩涩视频在线看 | 成人精品在线观看 | 亚洲伦理 | 久久久久一区 | 婷婷久久综合九色综合绿巨人 | 亚洲成av人影片在线观看 | 欧美日韩不卡 | 国产精品综合 | 亚洲国产高清在线 | 特黄特色的大片观看免费视频 | 午夜视频在线观看一区二区三区 | 天堂在线中文字幕 | 欧美黄色一级 | 日韩精品www | 91色乱码一区二区三区 | 日韩成人免费 | 欧美日韩精品久久久 | 久热精品免费视频 | 在线观看中文字幕亚洲 | 久久精品国产清自在天天线 | 日韩免费视频 | 国产精品久久久久久久久免费高清 | 国产精品久久久亚洲 | 久久久久一区二区三区 | 亚洲国产精品一区二区第一页 | a免费视频 | 99久久精品国产一区二区三区 | 国产一区二区三区在线免费 | 国产91精品一区二区绿帽 | 在线观看 亚洲 | 日韩中文字幕在线观看 | 中文在线一区二区 | 久久福利电影 | 久久亚洲欧美日韩精品专区 | 午夜在线电影 | 99热在线播放 | 二区影院|