使用springboot单元测试对weblistener的加载测试

目录
  • springboot单元测试对weblistener的加载测试
    • 原监听器代码
    • 测试类
  • springboot web做单元测试

springboot单元测试对weblistener的加载测试

使用spring-boot对web项目进行测试时对weblistener进行加载.以proxool连接池的加载为例.

原监听器代码

@WebListener
       public class ProxoolListener implements ServletContextListener{
       @Override
       public void contextDestroyed(ServletContextEvent arg0) {

       }
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
        loadProxool();

        }
       ...//其他实现方法
       }

spring-boot测试适配修改,继承TestExcutionListener接口,实现prepareTestInstance方法,将监听业务同样放在此方法中做预处理即可。

@WebListener
      public class ProxoolListener implements ServletContextListener,TestExecutionListener{
  @Override
  public void contextDestroyed(ServletContextEvent arg0) {
  }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
  loadProxool();
 }

 @Override
 public void afterTestClass(TestContext arg0) throws Exception {
  // TODO 自动生成的方法存根
 }

 @Override
 public void afterTestMethod(TestContext arg0) throws Exception {
  // TODO 自动生成的方法存根

 }

 @Override
 public void beforeTestClass(TestContext arg0) throws Exception {
  // TODO 自动生成的方法存根
 }

 @Override
 public void beforeTestMethod(TestContext arg0) throws Exception {
  // TODO 自动生成的方法存根
 }

 @Override
 public void prepareTestInstance(TestContext arg0) throws Exception {
                //启动测试时需要预先的处理
  loadProxool();
 }
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners(listeners = { ProxoolListener.class , DependencyInjectionTestExecutionListener.class })
public class DemoApplicationTest {
 @Test
 public void exampleTest() {
  try {
   System.out.println("Connection is closed : "+ProxoolUtility.getConnection("proxool.iovdc").isClosed());
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

springboot web做单元测试

package com.ziroom.finance.mbs.web;
import com.alibaba.fastjson.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
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.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
/**
 * 类说明    :MockMvc 测试web
 * 作者      :liuys
 * 日期      :2017/10/11  10:50
 * 版本号    : V1.0
 */
@RunWith(SpringJUnit4ClassRunner.class)
//开启web上下文测试
@WebAppConfiguration
@SpringBootTest
public class LoginControllerTest {
    //注入webApplicationContext
    @Autowired
    private WebApplicationContext webApplicationContext;
    private MockMvc mockMvc;
    //设置mockMvc
    @Before
    public void setMockMvc() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }
    @Test
    public void login(){
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("userName", "liuys26");
            jsonObject.put("userPw", "123");
            jsonObject.put("cityCode", "801000");
            jsonObject.put("userType", "0");
            mockMvc.perform(MockMvcRequestBuilders.post("/api/login/auth")
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(jsonObject.toJSONString())
            ).andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 详解Spring Boot实战之单元测试

    本文介绍使用Spring测试框架提供的MockMvc对象,对Restful API进行单元测试 Spring测试框架提供MockMvc对象,可以在不需要客户端-服务端请求的情况下进行MVC测试,完全在服务端这边就可以执行Controller的请求,跟启动了测试服务器一样. 测试开始之前需要建立测试环境,setup方法被@Before修饰.通过MockMvcBuilders工具,使用WebApplicationContext对象作为参数,创建一个MockMvc对象. MockMvc对象提供一组工具

  • Spring Boot 单元测试和集成测试实现详解

    学习如何使用本教程中提供的工具,并在 Spring Boot 环境中编写单元测试和集成测试. 1. 概览 本文中,我们将了解如何编写单元测试并将其集成在 Spring Boot 环境中.你可在网上找到大量关于这个主题的教程,但很难在一个页面中找到你需要的所有信息.我经常注意到初级开发人员混淆了单元测试和集成测试的概念,特别是在谈到 Spring 生态系统时.我将尝试讲清楚不同注解在不同上下文中的用法. 2. 单元测试 vs. 集成测试 维基百科是这么说单元测试的: 在计算机编程中,单元测试是一种

  • springboot单元测试两种方法实例详解

    这篇文章主要介绍了springboot单元测试两种方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 springboot的单元测试,这里介绍两种方式,一种是在测试类中添加注解:另一种是在代码中启动项目的main方法中继承接口(也可以写在其他方法中). 如 对查看数据库的连接池信息 进行单元测试 1. 在类上使用注解: @RunWith(SpringRunner.class) @SpringBootTest @RunWith(Sprin

  • springboot使用单元测试实战

    前言 springboot提供了 spirng-boot-starter-test 以供开发者使用单元测试,在引入 spring-boot-starter-test 依赖后: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope>

  • 使用springboot单元测试对weblistener的加载测试

    目录 springboot单元测试对weblistener的加载测试 原监听器代码 测试类 springboot web做单元测试 springboot单元测试对weblistener的加载测试 使用spring-boot对web项目进行测试时对weblistener进行加载.以proxool连接池的加载为例. 原监听器代码 @WebListener public class ProxoolListener implements ServletContextListener{ @Override

  • springboot启动时是如何加载配置文件application.yml文件

    今天启动springboot时,明明在resources目录下面配置了application.yml的文件,但是却读不出来,无奈看了下源码,总结一下springboot查找配置文件路径的过程,能力有限,欢迎各位大牛指导!!! spring加载配置文件是通过listener监视器实现的,在springboot启动时: 在容器启动完成后会广播一个SpringApplicationEvent事件,而SpringApplicationEvent事件是继承自ApplicationEvent时间的,代码如下

  • 详解springboot启动时是如何加载配置文件application.yml文件

    今天启动springboot时,明明在resources目录下面配置了application.yml的文件,但是却读不出来,无奈看了下源码,总结一下springboot查找配置文件路径的过程,能力有限,欢迎各位大牛指导!!! spring加载配置文件是通过listener监视器实现的,在springboot启动时: 在容器启动完成后会广播一个SpringApplicationEvent事件,而SpringApplicationEvent事件是继承自ApplicationEvent时间的,代码如下

  • Springboot常用注解及配置文件加载顺序详解

    Springboot常用注解及底层实现 1.@SpringBootApplication:这个注解标识了一个SpringBoot工程,她实际上是另外三个注解的组合,分别是: @SpringBootConfiguration:源码可以看到,这个注解除了元注解外,实际就只有一个@Configuration,把该类变成一个配置类,表示启动类也是一个配置类: @EnableAutoConfiguration:是开启自动配置的功能,向Spring容器中导入了一个Selector,用来加载ClassPath

  • springboot默认的5种加载路径详解

    目录 前言 一.application.properties/.yml文件初识 二.application.properties/.yml文件可以在其他路径吗 三.总结 前言 上次分享了如何一步一步搭建一个springboot的项目,详细参见<5分钟快速搭建一个springboot的项目>,最终的结果是在”8080“端口搭建起了服务,并成功访问.不知道有小伙伴是否有疑惑,springboot应该有配置文件的,一般的配置文件都是application.properties或者applicatio

  • SpringBoot使用Shiro实现动态加载权限详解流程

    目录 一.序章 二.SpringBoot集成Shiro 1.引入相关maven依赖 2.自定义Realm 3.Shiro配置类 三.shiro动态加载权限处理方法 四.shiro中自定义角色与权限过滤器 1.自定义uri权限过滤器 zqPerms 2.自定义角色权限过滤器 zqRoles 3.自定义token过滤器 五.项目中会用到的一些工具类常量等 1.Shiro工具类 2.Redis常量类 3.Spring上下文工具类 六.案例demo源码 一.序章 基本环境 spring-boot 2.1

  • SpringBoot用配置影响Bean加载@ConditionalOnProperty

    目录 故事背景 调试&解决 SpringBoot 是怎么做的 故事的最后 故事背景 故事发生在几个星期前,自动化平台代码开放给整个测试团队以后,越来越多的同事开始探索平台代码.为了保障自动化测试相关的数据和沉淀能不被污染,把数据库进行了隔离.终于有了2个数据库实例,一个给dev环境用,一个给test环境用.可是随着平台的发展,越来越多的中间件被引用了.所以需要隔离的东西就越来越多了,比如MQ,Redis等.成本越来越高,如果像数据库实例一样全部分开搞一套,那在当下全域降本增效的大潮下,也是困难重

  • springboot中的静态资源加载顺序优先级

    目录 springboot静态资源加载顺序优先级 看springboot源码里面 springboot静态资源加载规则 一.静态资源映射规则 1.webjars 2.springboot内置默认访问路径 3.首页处理 4.网站图标 springboot静态资源加载顺序优先级 看springboot源码里面 springboot静态资源加载规则 我们经常会使用springboot创建web应用,在springboot中金静态资源是如何存放的呢? 一.静态资源映射规则 我们先创建一个springbo

  • springboot基于IDEA环境热加载与热部署教程

    目录 一.使用Jrebel插件 二.devtools实现热加载 1.1.引入devtools的maven依赖 1.2.设置IDEA 1.3.修改一下application.properties配置 1.4.LiveReload插件 1.5.最后测试一下 在实际的开发过程中,我们经常修改代码之后,手动的重启项目,手动刷新浏览器查看修改效果.那么有没有一种方式能够快速的.自动的帮我们将修改代码自动更新,避免手动重启,从而提高开发效率呢?肯定是有的,但是对于这个功能很多人对功能的叫法有争议,笔者查询了

  • Spring Junit单元测试加载配置文件失败问题

    JUnit是Java中最有名的单元测试框架,用于编写和运行可重复的测试,多数Java的开发环境都已经集成了JUnit作为单元测试的工具.好的单元测试能极大的提高开发效率和代码质量. 使用SpringJunit单元测试,通过@ContextConfiguration加载配置文件后,只会在src/test/resources目录下寻找配置文件,不会加载src/main/resources中的. 这样就导致了项目可以正常启动,但是单元测试时会提示找不到注入的类. 可以通过pom.xml配置来解决该问题

随机推荐