首页 > 解决方案 > 如何以 vertx-junit5 的方式放置关于 RunTestOnContext 的 vertx @Rule?

问题描述

我想知道是否可以将这种规则(junit)迁移到 vertx-junit5 方式。原始示例是来自 github 中公共 vertx-example 存储库的 RunOnContextTest.java。这是代码:

@RunWith(VertxUnitRunner.class)
public class RunOnContextTest {

  /*
   * This rule wraps the junit calls in a Vert.x context, the Vert.x instance can be created by the
   * rule or provided like in this case.
   */
  @Rule
  public final RunTestOnContext rule = new RunTestOnContext(Vertx::vertx);

  private Thread thread;

  @Before
  public void before(TestContext context) {
    context.assertTrue(Context.isOnEventLoopThread());
    thread = Thread.currentThread();
  }

  @Test
  public void theTest(TestContext context) {
    context.assertTrue(Context.isOnEventLoopThread());
    context.assertEquals(thread, Thread.currentThread());
  } 
 @After
  public void after(TestContext context) {
    context.assertTrue(Context.isOnEventLoopThread());
    context.assertEquals(thread, Thread.currentThread());
  }

突出显示的依赖项是:

 <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-unit</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

标签: javaunit-testingvert.xjunit5junit5-extension-model

解决方案


Vert.x 最近增加了对此的支持。

使用 Vert.x 4.2.0,您可以使用:

  @RegisterExtension
  public final RunTestOnContext rt = new RunTestOnContext();

github中的参考-> https://github.com/vert-x3/vertx-junit5/issues/100


推荐阅读