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

來源:互聯網
上載者:User
概述

@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. 編寫啟動入口類

 
@SpringBootApplicationpublic class StartUpApplication {    public static void main(String[] args) {        SpringApplication.run(StartUpApplication.class, args);    }}

3. 編寫Controller類

 
@RestControllerpublic 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"地址發送請求,並列印返回結果     * @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 版本的提升,單元測試變得更簡單了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.