Java SpringBoot Test 单元测试中包括多线程时,没跑完就结束了

Posted VipSoft

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java SpringBoot Test 单元测试中包括多线程时,没跑完就结束了相关的知识,希望对你有一定的参考价值。

如何阻止 Java SpringBoot Test 单元测试中包括多线程时,没跑完就结束了
使用 CountDownLatch
CountDownLatch、CyclicBarrier 使用区别
多线程 ThreadPoolTaskExecutor 应用
Java BasePooledObjectFactory 对象池化技术

@SpringBootTest
public class PoolTest 

    @Test
    void basePooledTest() throws InterruptedException 

        AtomicInteger atomicInteger = new AtomicInteger();

        int excutorCount = 15;
        CountDownLatch countDownLatch = new CountDownLatch(excutorCount);

        // =====================创建线程池=====================
        ExecutorService excutor = Executors.newFixedThreadPool(5);
        // =====================创建对象池=====================
        // 对象池工厂
        MqttConnectionFactory personPoolFactory = new MqttConnectionFactory();
        // 对象池配置
        GenericObjectPoolConfig<MqttConnection> objectPoolConfig = new GenericObjectPoolConfig<>();
        objectPoolConfig.setMaxTotal(50);
        // 对象池
        GenericObjectPool<MqttConnection> mqttPool = new GenericObjectPool<>(personPoolFactory, objectPoolConfig);
        // =====================测试对象池=====================
        // 循环100次,从线程池中取多个多线程执行任务,来测试对象池
        for (int i = 0; i < excutorCount; i++) 
            excutor.submit(new Thread(() -> 
                // 模拟从对象池取出对象,执行任务
                MqttConnection mqtt = null;
                try 
                    // 从对象池取出对象
                    mqtt = mqttPool.borrowObject();
                    // 让对象工作
                    int count = atomicInteger.addAndGet(1);
                    mqtt.publish("Id:" + count + " Time: " + DateUtil.now());
                 catch (Exception e) 
                    e.printStackTrace();
                 finally 
                    // 回收对象到对象池
                    if (mqtt != null) 
                        mqttPool.returnObject(mqtt);
                    
                    countDownLatch.countDown();
                
            ));
        
        countDownLatch.await();
    


ABAP和Java SpringBoot的单元测试

ABAP

在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法,
技术分享图片

在单元测试启动后会自动被调用到。

Spring Boot

在Spring及Spring Boot “Convention over configuration”的设定思路里,放在路径src/test/java下面以Tests.java结尾的Java类会被当成单元测试类处理。
技术分享图片

对上述项目执行命令行mvn clean install后,报错误消息:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
at org.springframework.util.Assert.state(Assert.java:70)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:202)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:137)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:82)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

从调用栈能看出,Spring Boot单元测试框架试图自动找到一个被施加单元测试的Java类,但是失败了,所以报了异常。
技术分享图片

由于我这个SpringBoot项目的入口是com.sap.smartService.SmartServiceApplication, 因此我需要在单元测试启动类里指定这个入口类:
@SpringBootTest(classes = com.sap.smartService.SmartServiceApplication.class)
技术分享图片

加上之后maven build成功:
技术分享图片

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

技术分享图片







以上是关于Java SpringBoot Test 单元测试中包括多线程时,没跑完就结束了的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot项目单元测试

ABAP和Java SpringBoot的单元测试

如何给一个SpringBoot项目添加单元测试代码

单元测试 springboot-test

单元测试 springboot-test

JUnitJava 单元测试框架 | 学习笔记