Correct way to fail a unit test from within a callback using Vertx Unit











up vote
1
down vote

favorite












Given the following unit test, which uses the Vertx Unit testing framework:



@RunWith(VertxUnitRunner.class)
public class VertxUnitTest {

private Vertx vertx;

@Rule
public RunTestOnContext rule = new RunTestOnContext(new VertxOptions().setClustered(false)
.setClusterManager(new HazelcastClusterManager()).setMaxEventLoopExecuteTime(2000000000000L)
.setMaxWorkerExecuteTime(60000000000000L).setBlockedThreadCheckInterval(1000000)
.setEventBusOptions(new EventBusOptions().setClustered(false).setIdleTimeout(0)));

@Before
public void setup() throws Exception {
io.vertx.core.Vertx v = rule.vertx();
vertx = Vertx.newInstance(v);
}

private class MyVerticle extends AbstractVerticle {}

@Test
public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
Async async = context.async();

vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
c.cause();
vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
async.complete();
fail();

});
});
}
}


the call to fail() is throwing an exception to the console, but it is not actually failing the test itself, which finishes successfully and is green.



The same is true when working with Mockito. I can successfully verify the behavior of the verticle and its dependencies using mocks, but even when the Mockito assertions fail, the test itself will still pass. Calling fail on the vertx TestContext object - context.fail() - will also not fail the test.



The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.



Without the call to async.complete(), the test will it appears never complete.



What is the correct approach to this?



Thanks










share|improve this question




























    up vote
    1
    down vote

    favorite












    Given the following unit test, which uses the Vertx Unit testing framework:



    @RunWith(VertxUnitRunner.class)
    public class VertxUnitTest {

    private Vertx vertx;

    @Rule
    public RunTestOnContext rule = new RunTestOnContext(new VertxOptions().setClustered(false)
    .setClusterManager(new HazelcastClusterManager()).setMaxEventLoopExecuteTime(2000000000000L)
    .setMaxWorkerExecuteTime(60000000000000L).setBlockedThreadCheckInterval(1000000)
    .setEventBusOptions(new EventBusOptions().setClustered(false).setIdleTimeout(0)));

    @Before
    public void setup() throws Exception {
    io.vertx.core.Vertx v = rule.vertx();
    vertx = Vertx.newInstance(v);
    }

    private class MyVerticle extends AbstractVerticle {}

    @Test
    public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
    Async async = context.async();

    vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
    c.cause();
    vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
    async.complete();
    fail();

    });
    });
    }
    }


    the call to fail() is throwing an exception to the console, but it is not actually failing the test itself, which finishes successfully and is green.



    The same is true when working with Mockito. I can successfully verify the behavior of the verticle and its dependencies using mocks, but even when the Mockito assertions fail, the test itself will still pass. Calling fail on the vertx TestContext object - context.fail() - will also not fail the test.



    The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.



    Without the call to async.complete(), the test will it appears never complete.



    What is the correct approach to this?



    Thanks










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Given the following unit test, which uses the Vertx Unit testing framework:



      @RunWith(VertxUnitRunner.class)
      public class VertxUnitTest {

      private Vertx vertx;

      @Rule
      public RunTestOnContext rule = new RunTestOnContext(new VertxOptions().setClustered(false)
      .setClusterManager(new HazelcastClusterManager()).setMaxEventLoopExecuteTime(2000000000000L)
      .setMaxWorkerExecuteTime(60000000000000L).setBlockedThreadCheckInterval(1000000)
      .setEventBusOptions(new EventBusOptions().setClustered(false).setIdleTimeout(0)));

      @Before
      public void setup() throws Exception {
      io.vertx.core.Vertx v = rule.vertx();
      vertx = Vertx.newInstance(v);
      }

      private class MyVerticle extends AbstractVerticle {}

      @Test
      public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
      Async async = context.async();

      vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
      c.cause();
      vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
      async.complete();
      fail();

      });
      });
      }
      }


      the call to fail() is throwing an exception to the console, but it is not actually failing the test itself, which finishes successfully and is green.



      The same is true when working with Mockito. I can successfully verify the behavior of the verticle and its dependencies using mocks, but even when the Mockito assertions fail, the test itself will still pass. Calling fail on the vertx TestContext object - context.fail() - will also not fail the test.



      The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.



      Without the call to async.complete(), the test will it appears never complete.



      What is the correct approach to this?



      Thanks










      share|improve this question















      Given the following unit test, which uses the Vertx Unit testing framework:



      @RunWith(VertxUnitRunner.class)
      public class VertxUnitTest {

      private Vertx vertx;

      @Rule
      public RunTestOnContext rule = new RunTestOnContext(new VertxOptions().setClustered(false)
      .setClusterManager(new HazelcastClusterManager()).setMaxEventLoopExecuteTime(2000000000000L)
      .setMaxWorkerExecuteTime(60000000000000L).setBlockedThreadCheckInterval(1000000)
      .setEventBusOptions(new EventBusOptions().setClustered(false).setIdleTimeout(0)));

      @Before
      public void setup() throws Exception {
      io.vertx.core.Vertx v = rule.vertx();
      vertx = Vertx.newInstance(v);
      }

      private class MyVerticle extends AbstractVerticle {}

      @Test
      public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
      Async async = context.async();

      vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
      c.cause();
      vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
      async.complete();
      fail();

      });
      });
      }
      }


      the call to fail() is throwing an exception to the console, but it is not actually failing the test itself, which finishes successfully and is green.



      The same is true when working with Mockito. I can successfully verify the behavior of the verticle and its dependencies using mocks, but even when the Mockito assertions fail, the test itself will still pass. Calling fail on the vertx TestContext object - context.fail() - will also not fail the test.



      The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.



      Without the call to async.complete(), the test will it appears never complete.



      What is the correct approach to this?



      Thanks







      asynchronous junit junit4 vert.x vertx-verticle






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 8:18

























      asked Nov 19 at 16:57









      user1052610

      1,26042854




      1,26042854
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          the correct approach is to call the TestContext.fail() method, like so:



          @Test
          public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
          Async async = context.async();

          vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
          if(c.succeeded()) {
          vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
          if(rpl.succeeded()) {
          // make assertions based on reply contents, and then...
          async.complete();

          } else {
          context.fail(rpl.cause());
          }
          });

          } else {
          context.fail(c.cause());
          }
          });
          }





          share|improve this answer























          • Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
            – user1052610
            Nov 20 at 8:00










          • sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
            – homerman
            Nov 20 at 18:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379368%2fcorrect-way-to-fail-a-unit-test-from-within-a-callback-using-vertx-unit%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          the correct approach is to call the TestContext.fail() method, like so:



          @Test
          public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
          Async async = context.async();

          vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
          if(c.succeeded()) {
          vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
          if(rpl.succeeded()) {
          // make assertions based on reply contents, and then...
          async.complete();

          } else {
          context.fail(rpl.cause());
          }
          });

          } else {
          context.fail(c.cause());
          }
          });
          }





          share|improve this answer























          • Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
            – user1052610
            Nov 20 at 8:00










          • sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
            – homerman
            Nov 20 at 18:32















          up vote
          3
          down vote



          accepted










          the correct approach is to call the TestContext.fail() method, like so:



          @Test
          public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
          Async async = context.async();

          vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
          if(c.succeeded()) {
          vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
          if(rpl.succeeded()) {
          // make assertions based on reply contents, and then...
          async.complete();

          } else {
          context.fail(rpl.cause());
          }
          });

          } else {
          context.fail(c.cause());
          }
          });
          }





          share|improve this answer























          • Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
            – user1052610
            Nov 20 at 8:00










          • sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
            – homerman
            Nov 20 at 18:32













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          the correct approach is to call the TestContext.fail() method, like so:



          @Test
          public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
          Async async = context.async();

          vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
          if(c.succeeded()) {
          vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
          if(rpl.succeeded()) {
          // make assertions based on reply contents, and then...
          async.complete();

          } else {
          context.fail(rpl.cause());
          }
          });

          } else {
          context.fail(c.cause());
          }
          });
          }





          share|improve this answer














          the correct approach is to call the TestContext.fail() method, like so:



          @Test
          public void runFlow_correctMessage_stepsCalledInCorrectOrder(TestContext context) {
          Async async = context.async();

          vertx.getDelegate().deployVerticle(new MyVerticle(), new DeploymentOptions().setWorker(true), c -> {
          if(c.succeeded()) {
          vertx.eventBus().<Object>send("", new JsonObject(), new DeliveryOptions(), rpl -> {
          if(rpl.succeeded()) {
          // make assertions based on reply contents, and then...
          async.complete();

          } else {
          context.fail(rpl.cause());
          }
          });

          } else {
          context.fail(c.cause());
          }
          });
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 18:31

























          answered Nov 20 at 1:53









          homerman

          2,0021819




          2,0021819












          • Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
            – user1052610
            Nov 20 at 8:00










          • sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
            – homerman
            Nov 20 at 18:32


















          • Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
            – user1052610
            Nov 20 at 8:00










          • sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
            – homerman
            Nov 20 at 18:32
















          Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
          – user1052610
          Nov 20 at 8:00




          Could you expand your answer to show where the code to sent a message on the event bus (and associated callback) should go. My tests are within the callback associated with sending to the event bus.The core issue is this: any call to fail() after async.complete() will not fail the test, only the console will show the error. But without the call to async.complete(), the code in the verticle (called upon consuming from the event bus), will not have run before the test assertions are called.
          – user1052610
          Nov 20 at 8:00












          sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
          – homerman
          Nov 20 at 18:32




          sorry i overlooked that part of your original post. i've updated my answer to reflect the nested EventBus handling
          – homerman
          Nov 20 at 18:32


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379368%2fcorrect-way-to-fail-a-unit-test-from-within-a-callback-using-vertx-unit%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          If I really need a card on my start hand, how many mulligans make sense? [duplicate]

          Alcedinidae

          Can an atomic nucleus contain both particles and antiparticles? [duplicate]