Maven test多线程陷阱

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/j16421881/article/details/78638768

项目使用了maven-surefire-plugin运行Junit测试。
一段时间后发现一些运行失败的单元测试只能通过maven test重现。
后来了解到maven-surefire-plugin默认是多线程执行Junit test的,而一些单元测试共用了相同的资源,没有实现线程安全。
重构单元测试的同时将maven-surefire-plugin配置为单线程执行Junit,避免出现线程问题。

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.9</version>
                    <configuration>
                <!--once就表示单线程挨个执行Junit test-->
                        <forkMode>once</forkMode>
                        <!--该插件是另起一个Jvm运行Junit test,不配置内存容易造成内存不足-->
                        <argLine>-Xms512m -Xmx1600m ${surefireArgLine}</argLine>
                    </configuration>
                </plugin>

猜你喜欢

转载自blog.csdn.net/j16421881/article/details/78638768