junit基础学习之-多线程测试(6)
步骤:
1、定义单个TestRunner
2、重载单个TestRunner的runTest()
3、定义TestRunner数组,并添加多个TestRunner
4、MultiThreadedTestRunner测试
1 @Test 2 public void MultiRequestsTest() { 3 // 步骤一:构造一个Runner 4 TestRunnable runner = new TestRunnable() { 5 @Override //步骤二:重载runTest函数 6 public void runTest() throws Throwable { 7 // 测试内容 8 request.setRequestURI("/postDoctors/userController/selectUserById"); 9 request.setMethod("post"); 10 11 try{ 12 13 final ModelAndView mav = userController.selectUserById( 1,request, response); 14 ModelAndViewAssert.assertModelAttributeAvailable(mav, "map"); 15 16 }catch (Exception e){ 17 e.printStackTrace(); 18 } 19 } 20 }; 21 int runnerCount = 100; 22
//步骤三:定义数组 23 //Rnner数组,想当于并发多少个。 24 TestRunnable[] trs = new TestRunnable[runnerCount]; 25 for (int i = 0; i < runnerCount; i++) { 26 trs[i] = runner; 27 } 28
//步骤四:MultiThreadedTestRunner测试 29 // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入 30 MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); 31 try { 32 // 开发并发执行数组里定义的内容 33 mttr.runTestRunnables(); 34 } catch (Throwable e) { 35 e.printStackTrace(); 36 } 37 }