欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用springboot單元測試對weblistener的加載測試

 更新時間:2021年10月14日 14:22:08   作者:NewshiJ  
這篇文章主要介紹了使用springboot單元測試對weblistener的加載測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

springboot單元測試對weblistener的加載測試

使用spring-boot對web項(xiàng)目進(jìn)行測試時對weblistener進(jìn)行加載.以proxool連接池的加載為例.

原監(jiān)聽器代碼

@WebListener
       public class ProxoolListener implements ServletContextListener{
       @Override
       public void contextDestroyed(ServletContextEvent arg0) {
 
       }
        @Override
        public void contextInitialized(ServletContextEvent arg0) {        
        loadProxool();
        
        }
       ...//其他實(shí)現(xiàn)方法
       }

spring-boot測試適配修改,繼承TestExcutionListener接口,實(shí)現(xiàn)prepareTestInstance方法,將監(jiān)聽業(yè)務(wù)同樣放在此方法中做預(yù)處理即可。

@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 {
                //啟動測試時需要預(yù)先的處理
  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;
    //設(shè)置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();
        }
    }
}

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • spring boot基于注解的聲明式事務(wù)配置詳解

    spring boot基于注解的聲明式事務(wù)配置詳解

    這篇文章主要介紹了spring boot基于注解的聲明式事務(wù)配置詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java中的System.arraycopy()淺復(fù)制方法詳解

    Java中的System.arraycopy()淺復(fù)制方法詳解

    這篇文章主要介紹了Java中的System.arraycopy()淺復(fù)制方法詳解,Java數(shù)組的復(fù)制操作可以分為深度復(fù)制和淺度復(fù)制,簡單來說深度復(fù)制,可以將對象的值和對象的內(nèi)容復(fù)制;淺復(fù)制是指對對象引用的復(fù)制,需要的朋友可以參考下
    2023-11-11
  • Java如何通過ssh遠(yuǎn)程連接主機(jī)并執(zhí)行命令

    Java如何通過ssh遠(yuǎn)程連接主機(jī)并執(zhí)行命令

    這篇文章主要介紹了Java如何通過ssh遠(yuǎn)程連接主機(jī)并執(zhí)行命令問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • SpringBoot中連接多個RabbitMQ的方法詳解

    SpringBoot中連接多個RabbitMQ的方法詳解

    這篇文章主要介紹了SpringBoot中連接多個RabbitMQ的方法詳解,要實(shí)現(xiàn) SpringBoot 連接多個 RabbitMQ,只能自定義重寫一些東西,分別配置才可以,下面一起來走一下試試,需要的朋友可以參考下
    2023-10-10
  • Linux下Hadoop?2.7.3?安裝搭建過程

    Linux下Hadoop?2.7.3?安裝搭建過程

    Hadoop實(shí)現(xiàn)了一個分布式文件系統(tǒng)(Hadoop?Distributed?File?System),簡稱HDFS,這篇文章給大家介紹Linux下Hadoop?2.7.3?安裝搭建過程,感興趣的朋友跟隨小編一起看看吧
    2021-11-11
  • JAVA利用接口實(shí)現(xiàn)多繼承問題的代碼實(shí)操演示

    JAVA利用接口實(shí)現(xiàn)多繼承問題的代碼實(shí)操演示

    Java語言并不支持多繼承,這是由于多繼承會帶來許多復(fù)雜的問題,例如"菱形問題"等,下面這篇文章主要給大家介紹了關(guān)于JAVA利用接口實(shí)現(xiàn)多繼承問題的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • SpringCloud微服務(wù)熔斷器Hystrix使用詳解

    SpringCloud微服務(wù)熔斷器Hystrix使用詳解

    這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個組件,在整個生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • SpringBoot入坑筆記之spring-boot-starter-web 配置文件的使用

    SpringBoot入坑筆記之spring-boot-starter-web 配置文件的使用

    本篇向小伙伴介紹springboot配置文件的配置,已經(jīng)全局配置參數(shù)如何使用的。需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-01-01
  • java中的快捷鍵小結(jié)

    java中的快捷鍵小結(jié)

    以下是myeclipse中的所有快捷鍵列表
    2013-03-03
  • mybatis中點(diǎn)擊mapper接口快速定位到對應(yīng)xml中sql方式

    mybatis中點(diǎn)擊mapper接口快速定位到對應(yīng)xml中sql方式

    這篇文章主要介紹了mybatis中點(diǎn)擊mapper接口快速定位到對應(yīng)xml中sql方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評論