Unit Testing vertx-web verticle...

894 views
Skip to first unread message

Deven Phillips

unread,
Sep 10, 2015, 3:51:51 PM9/10/15
to vert.x
I have a verticle which uses vertx-web to create a Router. The router registers 2 endpoints:

PUT /rest/entry
GET /rest/winner

I am trying to unit test this using the following tests:

package us.juggl.raffle;


import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientRequest;
import io.vertx.core.http.HttpClientResponse;
import io.vertx.ext.unit.Async;
import io.vertx.ext.unit.TestContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;


/**
 * Created by dphillips on 9/10/15.
 */

@RunWith(io.vertx.ext.unit.junit.VertxUnitRunner.class)
public class MainTest {
   
private Vertx vertx;


   
@Before
   
public void setUp() throws Exception {
        vertx
= Vertx.vertx();
        vertx
.deployVerticle(new Main());
   
}

   
@Test
   
public void testAddValidEntry(TestContext context) {
       
final Async async = context.async();
       
HttpClient client = vertx.createHttpClient();
       
HttpClientRequest req = client.put(8080, "127.0.0.1", "/rest/entry");
        req
.handler((HttpClientResponse response) -> {
            context
.assertNotNull(response);
            context
.assertTrue(response.statusCode()==202);
            async
.complete();
       
});
        req
.exceptionHandler(exception -> {
            context
.fail(exception.getLocalizedMessage());
            async
.complete();
       
});
        req
.end("{\"given_name\": \"Deven\", \"family_name\": \"Phillips\"}");
   
}

   
@Test
   
public void testGetWinner(TestContext context) {
       
final Async async = context.async();
        vertx
.createHttpClient().put(8080, "localhost", "/rest/entry").handler(response -> {
            context
.assertNotNull(response);
            vertx
.createHttpClient().get(8080, "localhost", "/rest/winner").handler(resp -> {
                context
.assertNotNull(resp);
                async
.complete();
           
}).end();
       
}).end("{\"given_name\": \"Deven\", \"family_name\": \"Phillips\"}");
   
}


   
@Test
   
public void testAddInvalidEntry(TestContext context) {
       
final Async async = context.async();
       
HttpClient client = vertx.createHttpClient();
       
HttpClientRequest req = client.put(8080, "localhost", "/rest/entry");
        req
.handler(response -> {
            context
.assertNotNull(response);
            async
.complete();
       
});
        req
.end("{\"given_nOME\": \"Deven\", \"family_nOME\": \"Phillips\"}");
   
}
}

The problem is that I always get a 404 error for every test. Any suggestions on what I am doing wrong?

The code works when I run the application and I get good responses (202 or 200 respectively), but not when doing the unit tests.

Thanks in advance!!!

Deven Phillips

Deven Phillips

unread,
Sep 10, 2015, 4:29:37 PM9/10/15
to vert.x
Also, full source code available at: https://github.com/JUGGL/raffle

Deven Phillips

unread,
Sep 10, 2015, 11:41:06 PM9/10/15
to vert.x
OK, I figured it out... The @Before method was causing multiple instances of the Verticle to attempt to deploy and that caused a Bind exception because the port was in use... Added an @After method which shuts down Vert.x between tests and all is now passing...

Deven
Reply all
Reply to author
Forward
0 new messages