Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求

简介: Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求

测试表现层的代码如何测试

加载测试的专用属性

首先写一个测试

假定我们进行测试的时候要加一些属性

要去修改一些属性

我们可以写一个只在本测试有效的测试

写在配置里

测试

打印输出

我们把配置文件里面的配置注释掉后

我们同样可以启动

package com.example.demo;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest(properties = {"test.prop=多多"})
class DemoApplicationTests {
 
    @Value("${test.prop}")
    private String msg;
 
    @Test
    void contextLoads() {
        System.out.println(msg);
    }
 
}

我们可以为当前测试用例添加临时属性而不影响外部属性

properties可以为当前测试用例添加临时的属性配置

就是一种用参数的设置方式

类似于args

package com.example.demo;
 
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest(args = {"--test.prop=多多"})
class DemoApplicationTests {
 
    @Value("${test.prop}")
    private String msg;
 
    @Test
    void contextLoads() {
        System.out.println(msg);
    }
 
}

小结

可以应用与小范围的测试环境

避免对其他的测试环境产生影响

记载测试专用配置

临时加载一些bean能不能用呢

我们能不能把拦截器做成bean

把外部bean加入测试中

我们这边提供了一个bean

这个类是以第三方bean加入到我们的项目中的

package com.example.demo.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class MsgConfig {
    @Bean
    public String msg(){
        return "bean msg";
    }
 
}

Configuration注解表示是一个配置类

一个ioc容器

@Bean把对象做成bean给spring容器管理

package com.example.demo;
 
import com.example.demo.config.MsgConfig;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
 
@SpringBootTest
@Import(MsgConfig.class)
public class ConfigurationTest {
    
    @Autowired
    private String msg;
    
    @Test
    void testConfiguration(){
        System.out.println(msg);
    }
    
}v

如果测试时要用外部的bean

临时使用

我们直接用Import注解 做一个局部的配置导入

小结

应用于小范围的测试程序

测试类中启动web

我们的测试能不能测试表现层

SpringMVC 的东西呢

是可以的

我们首先要引入springMVC的依赖

坐标

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>3.2.5</version>
</dependency>

我们要在测试中启动web程序

默认的属性是这样的

不启动

启动

默认端口启动

package com.example.demo;
 
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class WebTest {
 
    @Test
    void test(){
 
    }
 
}

随机端口启动

我们的程序可以不写死端口

这样提高容错率

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

小结

发送虚拟请求

写一个表现层开发

Controller层

浏览器有响应

我们现在要在测试里发起一个请求

在测试里面调用spring MVC

该如何去呢

我们要做一个虚拟调用

//开启spirngMVC的虚拟调用
@AutoConfigureMockMvc

测试

package com.example.demo;
 
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.autoconfigure.web.servlet.MockMvcAutoConfiguration;
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.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启spirngMVC的虚拟调用
@AutoConfigureMockMvc
public class WebTest {
 
    @Test
    void test(@Autowired MockMvc mvc) throws Exception {
        //创建了一个虚拟请求
        //get类型的请求 访问路径为books
        MockHttpServletRequestBuilder bulider= MockMvcRequestBuilders.get("/books");
        mvc.perform(bulider);
    }
 
}

小结

目录
相关文章
|
4天前
|
Java Maven 开发工具
IDEA使用Spring Initializr流畅的创建springboot项目
IDEA使用Spring Initializr流畅的创建springboot项目
21 0
|
23小时前
|
IDE Java 机器人
Spring Boot中的多模块项目构建
Spring Boot中的多模块项目构建
|
3天前
|
消息中间件 XML Java
经验大分享:spring项目在启动的时候执行方法初始化
经验大分享:spring项目在启动的时候执行方法初始化
|
7天前
|
存储 弹性计算 Linux
阿里云账号注册、完成实名认证、试用云服务器和购买云服务器流程参考
本文为大家介绍新手用户从注册阿里云账号,完成实名认证,然后试用云服务器和购买云服务器的主要流程,适合初次购买和试用阿里云服务器的新手用户参考。
阿里云账号注册、完成实名认证、试用云服务器和购买云服务器流程参考
|
3天前
|
弹性计算 运维 安全
阿里云ecs使用体验
整了台服务器部署项目上线
|
4天前
|
存储 弹性计算 安全
阿里云服务器怎么样?云服务器ECS产品优势、应用场景、价格解析及常见问题参考
阿里云服务器ECS(Elastic Compute Service)是阿里云提供的性能卓越、稳定可靠、弹性扩展的IaaS(Infrastructure as a Service)级别云计算服务。把物理服务器比作买的房子,云服务器ECS,就是租赁的房子,阿里云云服务商就是管家。云服务商负责搭建机房、提供配套服务和维护,用户只需要付租金,即可“拎包入住”,无需自建机房、采购和配置硬件设施。如果不再需要云服务器,可随时“退租”(释放资源),节省成本。本文为大家解析云服务器ECS产品优势、应用场景和最新价格及常见问题。
阿里云服务器怎么样?云服务器ECS产品优势、应用场景、价格解析及常见问题参考
|
4天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得
|
5天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得
|
1天前
|
弹性计算
阿里云ECS的使用心得
本文主要讲述了我是如何了解到ECS,使用ECS的一些经验,以及自己的感悟心得
|
9天前
|
弹性计算
阿里云ECS使用体验
在申请高校学生免费体验阿里云ECS云服务器后的一些使用体验和感受。