Spring Boot单元测试:GroboUtils多线程测试

简介

  • Junit本身是不支持是多线程测试的,因为Junit的底层实现上,是用System.exit退出用例执行的。JVM终止了在测试线程启动的其他线程自然也无法执行。
  • GroboUtils旨在扩展Java测试的可能性。它包含许多子项目,通过Java进行不同方面的测试。GroboUtils中包含的流行工具包括多线程测试、层次单元测试和代码覆盖工具。
    这个工具集是在MIT许可下发布的。
  • 官网地址:groboutils

使用方法

1. 导入POM依赖

		<dependency>
			<groupId>net.sourceforge.groboutils</groupId>
			<artifactId>groboutils-core</artifactId>
			<version>5</version>
			<scope>test</scope>
		</dependency>

2. 编写测试用例

@SpringBootTest
class OrderServiceTest {

	@Autowired
    OrderMainStateService orderMainStateService;

	@Test
    public void sendOrderMainEvent() throws Throwable {
        // 生成所有测试线程
        int threadNum = 100;
        TestRunnable[] test = new TestRunnable[threadNum];
        long start = System.currentTimeMillis();
        for (int i = 0; i < test.length; i++) {
            test[i] = new TestRunnable() {
                @Override
                public void runTest() throws Throwable {
                	//业务代码
                	orderMainStateService.execute(MainEvents.CANCEL, "ordercode");
                }
            };
        }
        // 生成测试线程运行器
        MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(test);
        // 运行测试线程
        mttr.runTestRunnables();
        long used = System.currentTimeMillis() - start;
        System.out.printf("%s 调用花费 %s milli-seconds.\n", threadNum, used);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值