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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

NewshiJ   2021-10-14 我要评论
想了解使用springboot单元测试对weblistener的加载测试的相关内容吗,NewshiJ在本文为您仔细讲解springboot单元测试weblistener加载测试的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot单元测试,weblistener加载测试,下面大家一起来学习吧。

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

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们