Junit本是不支持多线程的,一个单元测试case主进程跑完,其他new出来的线程都会GG思密达。此篇mark一份在Junit中执行多线程的方法。
方案一
- 添加依赖pom.xml
<!-- https://mvnrepository.com/artifact/net.sourceforge.groboutils/groboutils-core -->
<dependency>
<groupId>net.sourceforge.groboutils</groupId>
<artifactId>groboutils-core</artifactId>
<version>5</version>
<scope>test</scope>
</dependency>
测试case
@Slf4j
public class DeviceMessageSolverTest extends JUnitBaseTest {
/**
* deviceInfoService <br>
*/
@Resource
private DeviceInfoService deviceInfoService;
/**
* deviceMessageSolver <br>
*/
@Resource
private DeviceMessageSolver deviceMessageSolver;
/**
* list <br>
*/
private static List<DeviceInfoBO> list;
/**
* appKeyArr <br>
*/
private final String[] appKeyArr = {
"2e2b380a56e7464aa678294c2c345545", "e2c85a1d245844fd9bdb14f0d0fc868a", "fa58f5306eb64ce094fe65fec6261587"
};
/**
* Description: 测试并发修改设备上下线<br>
*
* @author xubin<br>
* @taskId <br>
* <br>
*/
@Test
public void doStatusTest() {
try {
log.info("测试并发修改设备上下线");
DeviceInfoBO bo = new DeviceInfoBO();
bo.setAppKey(appKeyArr[0]);
PageInfo<DeviceInfoBO> page1 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED);
bo.setAppKey(appKeyArr[1]);
PageInfo<DeviceInfoBO> page2 = deviceInfoService.queryEntityListByPage(bo, 1, DigitConst.HUNDRED);
bo.setAppKey(appKeyArr[2]);
PageInfo<DeviceInfoBO> 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("测试并发修改设备上下线异常");
}
}
/**
* <Description> 线程任务<br>
*
* @author xubin<br>
* @version 1.0<br>
* @taskId <br>
* @CreateDate Nov 19, 2018 <br>
*/
class DeviceMsgTaskRunnable extends TestRunnable {
/**
* Description: 线程执行方法<br>
*
* @author xubin<br>
* @taskId <br>
* <br>
*/
@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();
- 执行结果截图