Vertx RoutingContext tests

711 views
Skip to first unread message

Ilya Ivanov

unread,
Mar 22, 2021, 7:01:06 AM3/22/21
to vert.x
Hi! I have implementation like:
Router router = Router.router(vertx);
router.get("/protected/user").handler(this::handleUserPage);
--------
private void handleUserPage(RoutingContext ctx) {
...
}

Is is possible to access RoutingContext User via jUnit or some else test frameworks?
Thanks!

Thomas SEGISMONT

unread,
Mar 22, 2021, 9:37:18 AM3/22/21
to vert.x
Hi,
In your test setup, you can create an HttpServer, a Router and register the required routes for the test case.
It's very cheap and relieves you from playing the mock game.
Regards

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/39d70de3-ebe5-463b-8bc3-f2574beb332cn%40googlegroups.com.

Ilya Ivanov

unread,
Mar 22, 2021, 9:43:17 AM3/22/21
to ve...@googlegroups.com
Doesn't that mean you are not triggering existing routes in this case?

пн, 22 мар. 2021 г. в 16:37, Thomas SEGISMONT <tsegi...@gmail.com>:


--
С уважением,
Иванов Илья,
+375 (29) 567-26-78
ООО "ГудСофт"

Thomas SEGISMONT

unread,
Mar 22, 2021, 9:55:44 AM3/22/21
to vert.x
In the case of a unit test, is it important to test all the routes that will present in the final application?

For an integration test yes, you can put them all.

Ilya Ivanov

unread,
Mar 22, 2021, 10:01:27 AM3/22/21
to vert.x
Sorry, I didn't mention that tests are integration ones. OK, got your idea, I have to create all needed routes at the test startup. Thanks!

понедельник, 22 марта 2021 г. в 16:55:44 UTC+3, tsegi...@gmail.com:

Paulo Lopes

unread,
Mar 24, 2021, 4:45:17 PM3/24/21
to vert.x

You can do something like this. In the setup phase create a http server and add your desired handlers/routes.

@RunWith(VertxUnitRunner.class)
public class RestApplicationTest {

  @Rule
  public static RunTestOnContext rule = new RunTestOnContext();

  private int port = 8081;
  private WebClient client;

  @Before
  public void before(TestContext should) {
    Async setup = should.async();

    Router app = Router.router(rule.vertx());

    // add the desired handlers/routes...

    rule.vertx()
      .createHttpServer()
      .requestHandler(app)
      .listen(0)
      .onFailure(should::fail)
      .onSuccess(server -> {
        port = server.actualPort();
        client = WebClient.create(rule.vertx());
        setup.complete();
      });
  }

  @After
  public void after() {
    client.close();
  }

  @Test
  public void callGreetingTest(TestContext should) {
    // Send a request and get a response
    Async test = should.async();
    client.get(port, "localhost", "/greeting")
      .send()
      .onFailure(should::fail)
      .onSuccess(response -> {
        should.assertEquals(401, response.statusCode());
        test.complete();
      });
  }
Reply all
Reply to author
Forward
0 new messages