eclipse-vertx / vertx-junit5

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

VertxTestContext#failNow(Throwable) not failing test #40

Closed matthewadams closed 6 years ago

matthewadams commented 6 years ago

Using

  <dependencies>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-rx-java</artifactId>
      <version>3.5.1</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.1.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.1.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-params</artifactId>
      <version>5.1.0</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-junit5</artifactId>
      <version>3.5.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

Reproducible test case:

package com.example;

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() throws InterruptedException {
    VertxTestContext context = new VertxTestContext();
    Vertx vertx = Vertx.vertx();
    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));
  }
}

If I run this test, it succeeds, when it should fail: image

jponge commented 6 years ago

Hi @matthewadams, VertxTestContext does not throw any exception.

You can check that the context is properly failed using context.failed() and context.causeOfFailure(). So what you should be doing is inspecting the context, and then making the test fail by throwing the exception.

Since you use the extension, you can benefit from parameter injection and test context management:

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

This fails properly, just as you would expect.

matthewadams commented 6 years ago

Nvm. Just saw your code. I’m glad it was StupidUserException!

On Fri, May 25, 2018 at 3:30 PM Julien Ponge notifications@github.com wrote:

Hi @matthewadams https://github.com/matthewadams, VertxTestContext does not throw an exception.

You can check that the context is properly failed using context.failed() and context.causeOfFailure(). So what you should be doing is inspecting the context, and then making the test fail by throwing the exception.

Since you use the extension, you can benefit from parameter injection and test context management:

@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); }

This fails properly, just as you would expect.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/vert-x3/vertx-junit5/issues/40#issuecomment-392173765, or mute the thread https://github.com/notifications/unsubscribe-auth/AAbdoBcM6U3xLAaMd3rjAE9c4Zem9Hxzks5t2Gn-gaJpZM4UOW19 .

matthewadams commented 6 years ago

FYI, filed related issue: https://github.com/vert-x3/vertx-junit5/issues/41