使用springboot單元測(cè)試對(duì)weblistener的加載測(cè)試
springboot單元測(cè)試對(duì)weblistener的加載測(cè)試
使用spring-boot對(duì)web項(xiàng)目進(jìn)行測(cè)試時(shí)對(duì)weblistener進(jìn)行加載.以proxool連接池的加載為例.
原監(jiān)聽(tīng)器代碼
@WebListener
public class ProxoolListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
loadProxool();
}
...//其他實(shí)現(xiàn)方法
}
spring-boot測(cè)試適配修改,繼承TestExcutionListener接口,實(shí)現(xiàn)prepareTestInstance方法,將監(jiān)聽(tīng)業(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)測(cè)試時(shí)需要預(yù)先的處理
loadProxool();
}
}
測(cè)試類(lèi)
@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做單元測(cè)試
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;
/**
* 類(lèi)說(shuō)明 :MockMvc 測(cè)試web
* 作者 :liuys
* 日期 :2017/10/11 10:50
* 版本號(hào) : V1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
//開(kāi)啟web上下文測(cè)試
@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 單元測(cè)試遇到的坑
- SpringBoot與單元測(cè)試JUnit的結(jié)合操作
- SpringBootTest單元測(cè)試報(bào)錯(cuò)的解決方案
- SpringBoot+JUnit5+MockMvc+Mockito單元測(cè)試的實(shí)現(xiàn)
- SpringBoot 單元測(cè)試實(shí)戰(zhàn)(Mockito,MockBean)
- SpringBoot @FixMethodOrder 如何調(diào)整單元測(cè)試順序
- SpringBoot環(huán)境下junit單元測(cè)試速度優(yōu)化方式
- 聊聊springboot2.2.3升級(jí)到2.4.0單元測(cè)試的區(qū)別
相關(guān)文章
Java構(gòu)造函數(shù)與普通函數(shù)用法詳解
本篇文章給大家詳細(xì)講述了Java構(gòu)造函數(shù)與普通函數(shù)用法以及相關(guān)知識(shí)點(diǎn),對(duì)此有興趣的朋友可以參考學(xué)習(xí)下。2018-03-03
springboot利用aop實(shí)現(xiàn)接口異步(進(jìn)度條)的全過(guò)程
我們?cè)陂_(kāi)發(fā)中,調(diào)用第三方接口時(shí),往往是提交數(shù)據(jù),要異步去獲取數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于springboot利用aop實(shí)現(xiàn)接口異步(進(jìn)度條)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-01-01
Java8中l(wèi)ambda表達(dá)式的應(yīng)用及一些泛型相關(guān)知識(shí)
這篇文章主要介紹了Java8中l(wèi)ambda表達(dá)式的應(yīng)用及一些泛型相關(guān)知識(shí)的相關(guān)資料2017-01-01
Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡(jiǎn)單使用方法
本文主要介紹了Mybatis中兼容多數(shù)據(jù)源的databaseId(databaseIdProvider)的簡(jiǎn)單使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07

