eclipse-vertx / vertx-junit5

Testing Vert.x applications with JUnit 5
Apache License 2.0
44 stars 32 forks source link

VertxExtension throwing when test method accepts io.vertx.rxjava.core.Vertx argument #41

Closed matthewadams closed 6 years ago

matthewadams commented 6 years ago

As a follow up to #40, I made the suggested fix:

import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import io.vertx.rxjava.core.Vertx;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import rx.Single;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

@ExtendWith(VertxExtension.class)
public class AuthenticatorTest {
  public static class AuthenticatorStub {
    public AuthenticatorStub(Vertx vertx) {}
    public Single<Token> getToken() {
      return Single.just(new Token().setValue("bogus").setExpiry(Instant.now().plusSeconds(60 * 5)));
    }
  }

  @Test
  public void testGetNewToken(Vertx vertx, VertxTestContext context) throws InterruptedException {
    AuthenticatorStub auth = new AuthenticatorStub(vertx);
    auth.getToken().subscribe(token -> {
      try {
        fail("bogus");
        context.completeNow();
      } catch (Throwable t) {
        context.failNow(t);
      }
    }, context::failNow);
    assertTrue(context.awaitCompletion(60, TimeUnit.SECONDS));
  }
}

This time, however, I get a ParameterResolutionException, because I'm accepting a io.vertx.rxjava.core.Vertx, not an io.vertx.core.Vertx:

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [io.vertx.rxjava.core.Vertx arg0] in executable [public void com.nml.ips.bnymellon.rest.AuthenticatorTest.testGetNewToken(io.vertx.rxjava.core.Vertx,io.vertx.junit5.VertxTestContext) throws java.lang.InterruptedException].

...

While I was able to workaround the issue with the code below, I feel like this module should optionally depend on io.vertx:vertx-rx-java (ie, use maven's scope provided or similar) and, if a test signature accepts an io.vertx.rxjava.core.Vertx, the extension should supply a new io.vertx.rxjava.core.Vertx(vertx), where vertx is the io.vertx.core.Vertx that the extension would normally supply.

Here's my workaround:

import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import io.vertx.rxjava.core.Vertx;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import rx.Single;

import java.time.Instant;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

@ExtendWith(VertxExtension.class)
public class AuthenticatorTest2 {
  public static class AuthenticatorStub {
    public AuthenticatorStub(Vertx vertx) {}
    public Single<Token> getToken() {
      return Single.just(new Token().setValue("bogus").setExpiry(Instant.now().plusSeconds(60 * 5)));
    }
  }

  public Vertx rxvertx;

  @BeforeEach
  public void setup(io.vertx.core.Vertx vertx) {
    rxvertx = new Vertx(vertx);
  }

  @Test
  public void testGetNewToken(VertxTestContext context) throws InterruptedException {
    AuthenticatorStub auth = new AuthenticatorStub(rxvertx);
    auth.getToken().subscribe(token -> {
      try {
        fail("bogus");
        context.completeNow();
      } catch (Throwable t) {
        context.failNow(t);
      }
    }, context::failNow);
    assertTrue(context.awaitCompletion(60, TimeUnit.SECONDS));
  }
}
jponge commented 6 years ago

This has already been implemented in the master branch and will be part of 3.6.0, see #33 and #34