Load test of weblistener using springboot unit test
- 2021-11-29 07:02:20
- OfStack
Directory springboot unit test unit test the load test of weblistener the original listener code test class springboot web
Load test of weblistener by springboot unit test
Load weblistener when testing the web project with spring-boot. Take the loading of proxool connection pool as an example.
Original listener code
@WebListener
public class ProxoolListener implements ServletContextListener{
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
loadProxool();
}
...// Other implementation methods
}
spring-boot test adapts to modify, inherits TestExcutionListener interface, realizes prepareTestInstance method, and pretreats listening services in this method.
@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 Automatically generated method stubs
}
@Override
public void afterTestMethod(TestContext arg0) throws Exception {
// TODO Automatically generated method stubs
}
@Override
public void beforeTestClass(TestContext arg0) throws Exception {
// TODO Automatically generated method stubs
}
@Override
public void beforeTestMethod(TestContext arg0) throws Exception {
// TODO Automatically generated method stubs
}
@Override
public void prepareTestInstance(TestContext arg0) throws Exception {
// Pre-processing is required when starting the test
loadProxool();
}
}
Test class
@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 for unit testing
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;
/**
* Class description :MockMvc Test web
* Author :liuys
* Date :2017/10/11 10:50
* Version number : V1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
// Open web Context testing
@WebAppConfiguration
@SpringBootTest
public class LoginControllerTest {
// Injection webApplicationContext
@Autowired
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
// Settings 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();
}
}
}