简体   繁体   中英

Vertx.io core vs reactivex verticle usage in jUnit

In my project mostly all imports relay on io.vertx.reactivex.core.Vertx package import, effectively makes whole project use reactivex (not core /vanilla) version of Vertx and it's verticles. I started to unit test a bit our application and to do so and to make it play nicely with JUint, according to this documentation following setup is needed to use JUnit and to run test cases in correct thread and verticle context:

@RunWith(VertxUnitRunner.class) /* Set the runner */
public class DaoTest {

    @Rule /* Define rule to get vertx instance in test */
    public RunTestOnContext rule = new RunTestOnContext();

   @Test
    public void exampleTest(TestContext context) {
       TestClass t = new TestClass(rule.vertx());
   }
}

the definition of TestClass is following:

import io.vertx.reactivex.core.Vertx; /* Mind here */
public class TestClass {

    public TestClass(Vertx vertx) {
        /* Something */
    }

I'm unable to provide correct instance of Vertx because only one definition of RunTestOnContext exist in package io.vertx.ext.unit.junit and produces io.vertx.core.Vertx instance, which is incompatible with io.vertx.reactivex.core.Vertx that TestClass is using. Some other test utilities, like TestContext have their equivalents in reactivex packages io.vertx.reactivex.ext.unit.TestContext , but this seems not be a case for RunTestOnContext .

The question would be how to obtain correctly io.vertx.reactivex.core.Vertx instance in test context to still ensure thread and context consistency?

The vertx-unit project has only depdendency to vertx-core . And has no dependency to vertx-rx-java . And that is understandable. Therefore the RunTestOnContext is built using the io.vertx.core.Vertx as you see.

You can downcast with the vertx.getDelegate() from io.vertx.reactivex.core.Vertx to io.vertx.core.Vertx . Bu that doesnt work in the opposite direction.

Therefore your best option is to copy the code of the RunTestOnContext and create your reactivex version of it. (The fastest way is to just change the import to io.vertx.reactivex.core.Vertx and use the vertx.getDelegate() where it is accessed.)

Get RxVertx instance by rxVertx = Vertx.vertx(); & vertx instance by rxVertx.getDelegate(); . Here is a full code snippet:

@RunWith(VertxUnitRunner.class)
public class MyVerticleTest {

   protected Vertx vertx;

   @Before
   public void setUp(TestContext context) {
      rxVertx = Vertx.vertx();
      vertx = rxVertx.getDelegate();
      Async async = context.async(1);
      RxHelper.deployVerticle(rxVertx, new MyVerticle())
            .subscribe(res -> async.countDown(), err -> {
               throw new RuntimeException(err);
            });
   }

   @Test
   public void my_test(TestContext context) {
      Async async = context.async();
      rxVertx.eventBus().rxSend("address", dataToSend)
           // .flatMap()
            .subscribe(receivedData -> {
                // assert what you want;
                 async.complete(); 
            }, context::fail);
   }

   @After
   public void tearDown(TestContext context) {
      client.close();
      rxVertx.close(context.asyncAssertSuccess());
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM