如何用springboot 做web的并发测试呢?

lazy cat 2017-12-21 11:31:39
我想用多线程模拟web的并发:
package com.mininglamp.hbjtt.controllerTest;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class MigrationMapControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
public void shouldReturnDefaultMessage() throws Exception {
for (int i = 0; i < 50; i++) {
MyThread thread = new MyThread();
Thread tt = new Thread(thread);
tt.start();
}
}

class MyThread implements Runnable {
@Override
public void run() {
try {
mockMvc.perform(get("/")).andExpect(status().isOk());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

出现了如下错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'spring.datasource-org.springframework.boot.autoconfigure.jdbc.DataSourceProperties': Initialization of bean failed; nested exception is java.lang.IllegalStateException: org.springframework.web.context.support.GenericWebApplicationContext@1f59a598 has been closed already
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)

那么怎么springboot该如何正确的测试并发呢?

...全文
546 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
土豆的老公 2017-12-26
  • 打赏
  • 举报
回复
你对API做测试就好啦。
yannsPeng 2017-12-21
  • 打赏
  • 举报
回复
不用压测工具吗。

67,518

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧