Spring boot 单元测试

其中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.
 */

@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());
    }

}

注意代码中的webEnvironment=RANDOM_PORT表示启动服务时端口随机(避免端口冲突),用@LocalServerPort注入端口。

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);

    }

}

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容