单元测试(三)——建立多线程单元测试

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

  • 单元测试(一)——SpringBoot建立单元测试
  • 单元测试(二)——Junit4+EasyMock建立单元测试
  • 单元测试(三)——建立多线程单元测试

Junit本是不支持多线程的,一个单元测试case主进程跑完,其他new出来的线程都会GG思密达。此篇mark一份在Junit中执行多线程的方法。

方案一

  • 添加依赖 pom.xml

        
            net.sourceforge.groboutils
            groboutils-core
            5
            test
        

测试case

@Slf4j
public class DeviceMessageSolverTest extends JUnitBaseTest {
	/**
     * deviceInfoService 
*/ @Resource private DeviceInfoService deviceInfoService; /** * deviceMessageSolver
*/ @Resource private DeviceMessageSolver deviceMessageSolver; /** * list
*/ private static List list; /** * appKeyArr
*/ private final String[] appKeyArr = { "2e2b380a56e7464aa678294c2c345545", "e2c85a1d245844fd9bdb14f0d0fc868a", "fa58f5306eb64ce094fe65fec6261587" }; /** * Description: 测试并发修改设备上下线
* * @author xubin
* @taskId
*
*/ @Test public void doStatusTest() { try { log.info("测试并发修改设备上下线"); DeviceInfoBO bo = new DeviceInfoBO(); bo.setAppKey(appKeyArr[0]); PageInfo page1 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED); bo.setAppKey(appKeyArr[1]); PageInfo page2 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED); bo.setAppKey(appKeyArr[2]); PageInfo page3 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED); list = page1.getRows(); list.addAll(page2.getRows()); list.addAll(page3.getRows()); log.info("已准备初始化数据:{} 条", list.size()); //线程数 int n = DigitConst.TEN; TestRunnable[] trs = new TestRunnable[n]; for (int i = 0; i < n; i++) { trs[i] = new DeviceMsgTaskRunnable(); } MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs); mttr.runTestRunnables(); } catch (Throwable e) { log.error("测试并发修改设备上下线异常", e); fail("测试并发修改设备上下线异常"); } } /** * 线程任务
* * @author xubin
* @version 1.0
* @taskId
* @CreateDate Nov 19, 2018
*/ class DeviceMsgTaskRunnable extends TestRunnable { /** * Description: 线程执行方法
* * @author xubin
* @taskId
*
*/ @Override public void runTest() { long startTime = System.currentTimeMillis();while (true) { try { //组装参数 DeviceInfoBO db = list.get((int) (Math.random() * list.size())); DeviceStatusDTO deviceStatusDTO = new DeviceStatusDTO(); deviceStatusDTO.setAppKey(db.getAppKey()); deviceStatusDTO.setTimeFlag(new Date().getTime()); deviceStatusDTO.setDeviceCode(db.getDeviceCode()); if (db.getIsOnline() == MainConstant.DEVICE_CURRENT_OFFLINE) { deviceStatusDTO.setStatus(true); } else { deviceStatusDTO.setStatus(false); } log.info(JSONObject.toJSONString(deviceStatusDTO)); deviceMessageSolver.doStatus(deviceStatusDTO); } catch (Exception e) { log.error("线程执行异常", e.getMessage()); } } } } }

关键代码:

1.class DeviceMsgTaskRunnable extends TestRunnable
2.TestRunnable[] trs = new TestRunnable[n];
3.for (int i = 0; i < n; i++) {
	  trs[i] = new DeviceMsgTaskRunnable();
   }
4.MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
5.mttr.runTestRunnables();
  • 执行结果截图

转载于:https://my.oschina.net/u/872813/blog/2878822

你可能感兴趣的:(单元测试(三)——建立多线程单元测试)