SpringBoot集成SpringMVC测试

SpringBoot集成测试

该篇博客会让你创建一个简单的spring应用并且使用Junit来测试它,怎么创建spring应用参考我创建一个简答你的springboot应用并且运行,您可能已经知道如何在应用程序中编写和运行单个类的单元测试,因此,对于本博客,我们将专注于使用Spring测试和Spring启动功能来测试Spring和代码之间的交互。您将首先从一个简单的测试开始,即应用程序上下文加载并继续使用Spring的MockMVC测试Web层

 

最终的项目结构如下:

HomeController类:

package com.ren.springboottest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HomeController {
    @RequestMapping(value = "/")
    public @ResponseBody String greeting() {
        return "HelloWorld!";
    }
}

1. 运行应用

点击打开SpringboottestApplication类,点击运行main方法,打开浏览器,访问http://localhost:8080,页面会显示HelloWorld,到这里应用应该是没有问题的

2.测试应用

使用SpringBoot的测试来模拟Http请求

在测试test中创建测试类HttpRequestTest,

SpringBootTest.WebEnvironment.RANDOM_PORT  :  随机生成一个端口,

@LocalServerport   :  将随机生成的端口注入给port

TestRestTemplate :     getForObject(...)用来访问控制器方法,会返回控制器方法的响应,然后使用断言,测试响应中是否包含Hello

package com.ren.springboottest;

import org.junit.jupiter.api.Test;
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.boot.web.server.LocalServerPort;
import static org.assertj.core.api.Assertions.assertThat;

// RANDOM_PORT会随机生成一个端口
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {
    @LocalServerPort  // 将随机生成的端口注入给port
    private int port;

    @Autowired
    private TestRestTemplate testRestTemplate;

    @Test
    public void testHttp() {
        String responseText = this.testRestTemplate.getForObject("http://localhost:" + port, String.class);
        assertThat(responseText.contains("Hello"));
    }
}

另外一种是根本不启动服务器仅仅测试试视图层,spring脱离controller来处理Http请求,这样几乎使用了完整的堆栈,你的代码就像我们在处理一个真的Http请求一样被调用并且不需要启动服务器,使用MocjkMvc就可以做到,通过在测试用例上使用@AutoConfigureMockMvc注解,下面的代码演示了怎么样做:

MVCTest类:

package com.ren.springboottest;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
public class MVCTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void testHttpReqeust() throws Exception {
        this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()).
                andExpect(content().string(containsString("Hello")));
    }
}

运行测试用例,将在控制台看到模拟的springMVC请求和响应的测试结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值