Spring Boot(七):spring boot测试介绍

首先maven要引入spring-boot-starter-test这个包。

先看一段代码

@RunWith(SpringRunner.class)

@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)

public class MyTest {

        @Autowired

    private TestRestTemplate restTemplate;

    @Test 

  public void test() {

        this.restTemplate.getForEntity( 

           "/{username}/vehicle", String.class, "Phil");

    }

}

1、@RunWith(SpringRunner.class) 告诉JUnit运行使用Spring的测试支持。SpringRunner是SpringJUnit4ClassRunner的新名字,这个名字只是让名字看起来简单些。

2、@SpringBootTest的意思是“带有Spring Boot支持的引导程序”(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)。

3、webEnvironment属性允许为测试配置特定的“网络环境”。你可以利用一个MOCK小服务程序环境开始你的测试,或者使用一个运行在RANDOM_PORT 或者 DEFINED_PORT上的真正的HTTP服务器。

4、如果我们想要加载一个特定的配置,我们可以用@SpringBootTest class属性。在这个实例中,我们省略classes就意味着测试要首次尝试从任意一个inner-classes中加载@ configuration,如果这个尝试失败了,它会在你主要的@SpringBootApplicationclass中进行搜索。

 ------------------------------------------------------------------------------------------------------------------------------

其中IndexController是你写的controller

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class IndexTest {


    private MockMvc mvc;

    @Autowired
    private IndexController indexController;


    @Before
    public void setup() {
        this.mvc = MockMvcBuilders.standaloneSetup(indexController).build();
    }

    @Test
    public void t() throws Exception {
        assertNotNull(mvc);

        mvc.perform(MockMvcRequestBuilders.get("/h").
                accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
                .andExpect(MockMvcResultMatchers.status().isOk());
        
    }
}

这个是第二种测试方法:

package com.hexun.bdc.auth.controller;

import com.hexun.bdc.auth.client.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;

/**
 * Created by iceblue on 2017/3/26.
webEnvironment=RANDOM_PORT表示启动服务时端口随机(避免端口冲突)
*/ @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class) @AutoConfigureMockMvc public class IndexControllerTest { @Autowired protected MockMvc mvc; @LocalServerPort // 注入端口 private Integer port; @Autowired private TestRestTemplate restTemplate; private static final Logger logger = LoggerFactory.getLogger(IndexControllerTest.class); @Test public void t() throws Exception { assertNotNull(mvc); logger.info("# port:" + port.toString()); assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/h", String.class)).contains("hello"); // mvc.perform(MockMvcRequestBuilders.get("/h")) // .andExpect(MockMvcResultMatchers.status().isOk()); } }

是第三种测试方法,直接利用TestRestTemplate类

package com.hexun.bdc.auth.controller;

import com.hexun.bdc.auth.client.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.test.context.junit4.SpringRunner;

import static org.junit.Assert.assertNotNull;


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = Application.class)
public class IndexTest2 {


    @Autowired
    private TestRestTemplate restTemplate;
    private static final Logger logger = LoggerFactory.getLogger(IndexTest2.class);


    @Test
    public void t() throws Exception {
        assertNotNull(restTemplate);
        String body = this.restTemplate.getForObject("/h", String.class);
        System.out.println("body = " + body);
        logger.info("body = " + body);

    }

}
原文地址:https://www.cnblogs.com/shamo89/p/7850366.html