使用springboot單元測試對weblistener的加載測試
springboot單元測試對weblistener的加載測試
使用spring-boot對web項(xiàng)目進(jìn)行測試時(shí)對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 自動(dòng)生成的方法存根
}
@Override
public void afterTestMethod(TestContext arg0) throws Exception {
// TODO 自動(dòng)生成的方法存根
}
@Override
public void beforeTestClass(TestContext arg0) throws Exception {
// TODO 自動(dòng)生成的方法存根
}
@Override
public void beforeTestMethod(TestContext arg0) throws Exception {
// TODO 自動(dòng)生成的方法存根
}
@Override
public void prepareTestInstance(TestContext arg0) throws Exception {
//啟動(dòng)測試時(shí)需要預(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();
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 解決@SpringBootTest 單元測試遇到的坑
- SpringBoot與單元測試JUnit的結(jié)合操作
- SpringBootTest單元測試報(bào)錯(cuò)的解決方案
- SpringBoot+JUnit5+MockMvc+Mockito單元測試的實(shí)現(xiàn)
- SpringBoot 單元測試實(shí)戰(zhàn)(Mockito,MockBean)
- SpringBoot @FixMethodOrder 如何調(diào)整單元測試順序
- SpringBoot環(huán)境下junit單元測試速度優(yōu)化方式
- 聊聊springboot2.2.3升級到2.4.0單元測試的區(qū)別
相關(guān)文章
Java構(gòu)造函數(shù)與普通函數(shù)用法詳解
本篇文章給大家詳細(xì)講述了Java構(gòu)造函數(shù)與普通函數(shù)用法以及相關(guān)知識點(diǎn),對此有興趣的朋友可以參考學(xué)習(xí)下。2018-03-03
springboot利用aop實(shí)現(xiàn)接口異步(進(jìn)度條)的全過程
我們在開發(fā)中,調(diào)用第三方接口時(shí),往往是提交數(shù)據(jù),要異步去獲取數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot利用aop實(shí)現(xiàn)接口異步(進(jìn)度條)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
Java8中l(wèi)ambda表達(dá)式的應(yīng)用及一些泛型相關(guān)知識
這篇文章主要介紹了Java8中l(wèi)ambda表達(dá)式的應(yīng)用及一些泛型相關(guān)知識的相關(guān)資料2017-01-01
Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡單使用方法
本文主要介紹了Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡單使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

