激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - 使用springboot單元測試對weblistener的加載測試

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

2022-02-20 11:47NewshiJ Java教程

這篇文章主要介紹了使用springboot單元測試對weblistener的加載測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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

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

原監(jiān)聽器代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@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ù)處理即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@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(); 
 }
}

測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
@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做單元測試

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/ikevin/article/details/78530097

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久久九九九女人毛片 | 二区成人| 亚洲一区二区三区视频免费 | 成人三级黄色片 | 龙的两根好大拔不出去h | 成人在线观看免费高清 | wwwxxx国产| 久久99精品久久久久久236 | 91看片儿 | 久草在线手机视频 | a网在线| 黄色美女网站免费看 | 毛片网站视频 | 色淫网站免费视频 | av成人免费看 | 国产99久久精品一区二区 | 日日草夜夜操 | 亚洲国产网站 | 丁香天堂网 | 免费a级黄色片 | 国产资源在线免费观看 | 极品xxxx欧美一区二区 | 精品国产精品久久 | 久草在线视频中文 | 久久男| 九九热精品在线视频 | 青草伊人网 | 天天操综 | 精品99在线视频 | 天天色狠狠干 | 国产精品一区二区三区在线看 | av在线免费观看不卡 | 91短视频在线 | 好吊色欧美一区二区三区四区 | 久久综合久久综合久久 | 91情侣偷在线精品国产 | 一级电影免费在线观看 | 欧美亚洲免费 | 国产精品99一区二区 | 九九热精品在线视频 | www.91在线观看 |