java - 使用注释将 Active TestSuite 切换到 JUnit 4

标签 java junit junit4

我创建了一些测试用例并使用 ActiveTestSuite 并行运行这些方法以检查一些潜在的线程问题。接下来,我想使用注释将类交换到 JUnit4,因为我需要利用 @BeforeClass 和 @AfterClass 注释。我的问题在于创建测试套件。以前,我做了以下操作

public static Test suite() {
       TestSuite suite = new ActiveTestSuite(
              MultithreadedTests.class.getName());
       // the parm is the method name!!!
       suite.addTest(new MultithreadedTests("testTransferToHostStockCountAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostRTVAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostTRFAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostRCVAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostCartonAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostInvAdjustmentAndFinFile"));
       suite.addTest(new MultithreadedTests("testTransferToHostStoreOrderAndFinFile"));
       return suite;
}

为了让课前/课后部分正常工作,我的测试类不再扩展 TestCase,否则那些方法不会被触发。我可以像这样编译我的类:

package com.jda.portfolio.hostexport.server.exporthandler;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ com.jda.portfolio.hostexport.server.exporthandler.TestThreadedExportHandler.class  })
public class AllTests {} 

但是测试是单线程的,这不是我想要的。我需要能够在单独的线程中运行每个测试用例方法,但我很难理解如何使用 JUnit4 注释来创建套件,使其成为“ActiveTestSuite”。

我错过了什么?

最佳答案

ActiveTestSuite 似乎仍然作为 junit 4 的一部分在包“junit.extensions”中可用。

还有一些第三方扩展可以做类似的事情。我使用了 these 3 classes 的变体在我的一个项目中成功。还有关于 Concurrent JUnit Tests With RunnerScheduler 的讨论听起来像是一个可能的解决方案。

关于java - 使用注释将 Active TestSuite 切换到 JUnit 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9025021/

相关文章:

java - 从 json 文件加载 spring-boot 属性

android - 无法在包 [android、org.robolectric.default] 中找到资源 ID #0x7f09001c

intellij-idea - IntelliJ在集成测试(Gradle项目)中不使用JUnit导入

android - 尝试在 Eclipse 中运行 Android JUnit 测试失败?

java - JUnit @before 执行程序

java - OSM xml 损坏?

java - PHP 对 Java 风格的类泛型有答案吗?

java - 错误。无法创建请求的服务[org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

java - 当方法调用与执行时完全相同时,EasyMock 对象会抛出意外的方法调用

android - 使用 JUnit + 的单元测试用例(Android 中的 Robolectric 或 Mockito 或两者)