Vertx unit-testing (vertx-unit) using configuration from spring-boot application.properties file

365 views
Skip to first unread message

Rimon Ashkenazy

unread,
Aug 30, 2016, 8:02:49 AM8/30/16
to vert.x
Hi, all.

I'm running Vertx (3.3.2) in a spring-boot application.
I wanted to write some unit and integration tests for my application, and followed the blogs/examples:

here is an example (from http://vertx.io/blog/unit-and-integration-tests/) of deploying a RestServer Verticle:

@Before
public void setUp(TestContext context) throws IOException {
  vertx = Vertx.vertx();
  ServerSocket socket = new ServerSocket(0);
  port = socket.getLocalPort();
  socket.close();
  DeploymentOptions options = new DeploymentOptions()
      .setConfig(new JsonObject().put("http.port", port)
      );
  vertx.deployVerticle(MyFirstVerticle.class.getName(), options, context.asyncAssertSuccess());
}

However, my application Verticle (which is also a RestServer) gets its http.port from a spring-boot application.properties file. 
So, I actually cannot use the above example of setting the "http.port" via the verticle config.
(currently the test fails with exception of NULL pointer - since the values from application.properties are NULL)

Can you recommend how to effectively get and use the spring-boot application.properties values in the vertx-unit testing ? 

Thanks,
Rimon

Thomas SEGISMONT

unread,
Aug 30, 2016, 8:11:12 AM8/30/16
to ve...@googlegroups.com
Just to make sure I understand: you RestServer verticle gets its http/port injected right? And you're wondering how to deploy an existing instance of the verticle (which you get from Spring container)? In this case you have two options:
- deploy by instance instead of deploying by class name
- create a custom verticle factory which will retrieve the verticle instance from the spring container instead of creating a fresh one.

--
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+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/24fffce1-37cf-4b3d-9b33-48bc23c2bb7e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rimon Ashkenazy

unread,
Aug 30, 2016, 9:19:05 AM8/30/16
to vert.x
Thank you for your quick answer.
1) I tried the first option - and the Test fails with NullPointerException in deploying the verticle (before this change, when deploying the class name - the verticle was deployed, but the Vaules from application.properties were all null).
2) I didn't quite understand option 2. Could you please elaborate how to create this factory ? and how to use it ?
Thanks.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Thomas SEGISMONT

unread,
Aug 30, 2016, 11:28:53 AM8/30/16
to ve...@googlegroups.com
2016-08-30 15:19 GMT+02:00 Rimon Ashkenazy <rimo...@gmail.com>:
Thank you for your quick answer.
1) I tried the first option - and the Test fails with NullPointerException in deploying the verticle (before this change, when deploying the class name - the verticle was deployed, but the Vaules from application.properties were all null).

I can't comment on the NPE without more context (code and stack trace).
 
2) I didn't quite understand option 2. Could you please elaborate how to create this factory ? and how to use it ?

Create your own io.vertx.core.spi.VerticleFactory, register your test resources META-INF/services folder. Your own VF would retrieve a verticle instance from the spring container OR, in test env, simply create a new instance of the class and manually inject values for test environment

 
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages