使用springboot單元測試對weblistener的加載測試
springboot單元測試對weblistener的加載測試
使用spring-boot對web項目進(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();
}
...//其他實現(xiàn)方法
}
spring-boot測試適配修改,繼承TestExcutionListener接口,實現(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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
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)程連接主機并執(zhí)行命令
這篇文章主要介紹了Java如何通過ssh遠(yuǎn)程連接主機并執(zhí)行命令問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
SpringCloud微服務(wù)熔斷器Hystrix使用詳解
這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個組件,在整個生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07
SpringBoot入坑筆記之spring-boot-starter-web 配置文件的使用
本篇向小伙伴介紹springboot配置文件的配置,已經(jīng)全局配置參數(shù)如何使用的。需要的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-01-01
mybatis中點擊mapper接口快速定位到對應(yīng)xml中sql方式
這篇文章主要介紹了mybatis中點擊mapper接口快速定位到對應(yīng)xml中sql方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

