Vertx Unit TEST - Retrieve config value

258 views
Skip to first unread message

sudhir kumar

unread,
Jan 12, 2017, 7:09:05 AM1/12/17
to vert.x
Hi Team,

 I followed the tutorial http://vertx.io/blog/unit-and-integration-tests . We are setting the configuration.  Then after deploying verticle with given DeploymentOption Set .Next tried to access the vertx.getOrCreateContext().config().getString(”KEY”) . The value of config was not found .


[5:36]  
https://github.com/vert-x3/vertx-unit/issues/38 , It seems to be issue!! Any Comments :slightly_smiling_face:


JsonObject obj = new JsonObject();
obj.put("mongoBaseURL","http://test1.com");
obj.put("NEW_API_URL", "http://test2.com");


options = new DeploymentOptions().setConfig(obj);

vertx.deployVerticle(QueueServiceVerticle.class.getName(), options, context.asyncAssertSuccess());

// S.O.P(vertx.getOrCreateContext().config().getString(” mongoBaseURL”)) -- is null !!

Julien Viet

unread,
Jan 13, 2017, 2:59:16 AM1/13/17
to ve...@googlegroups.com
Hi,

can you show your code, because even if you are pointing out the blog entry, it is hard to figure out how it fails in your particular case.

thanks

Julien

--
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.
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/4a5a6f68-c0b7-4162-9ed0-4727eb253563%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sudhir kumar

unread,
Jan 13, 2017, 5:57:57 AM1/13/17
to vert.x
Hi Julien , 

Thanks for replying.



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


I am deploying the Verticle and then next when trying to access config "vertx.getOrCreateContext().config().getString(” mongoBaseURL”) - The value is null . We can see deploymentID of verticle also.

Let me know if we can have a call for same, i can arrange .

Regards,
Sudhir 

Clement Escoffier

unread,
Jan 13, 2017, 6:56:38 AM1/13/17
to ve...@googlegroups.com
Hi,

In this code you are only passing the “http.port” in your config, not the “mongoBaseURL". 

Clement

-- 
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.
Visit this group at https://groups.google.com/group/vertx.

sudhir kumar

unread,
Jan 13, 2017, 8:39:38 AM1/13/17
to vert.x
Hi ,


Code snippet was mentioned in starting of thread.



JsonObject obj = new JsonObject();
obj.put("mongoBaseURL","http://test1.com");
obj.put("NEW_API_URL", "http://test2.com");


options = new DeploymentOptions().setConfig(obj);

vertx.deployVerticle(QueueServiceVerticle.class.getName(), options, context.asyncAssertSuccess());

// S.O.P(vertx.getOrCreateContext().config().getString(” mongoBaseURL”)) -- is null !!


--------------
@Before
public void setUp(TestContext context) throws IOException {

vertx = Vertx.vertx();
    // vertx = Vertx.vertx();
    JsonObject obj = new JsonObject();
    obj.put("mongoBaseURL","http://test.com");
obj.put("Queue_URI", "http://test.com");
obj.put("Queue_name", "Queue");


options = new DeploymentOptions().setConfig(obj);

vertx.deployVerticle(QueueServiceVerticle.class.getName(), options, context.asyncAssertSuccess());
    qc = new QueueController(vertx);
client = qc.createClientWithUri();
}

Clement Escoffier

unread,
Jan 13, 2017, 8:57:52 AM1/13/17
to ve...@googlegroups.com
Can you share your code in a Github project, so we can look at it. 

Clement

sudhir kumar

unread,
Jan 13, 2017, 9:33:37 AM1/13/17
to vert.x
Hi Clement,

I will create sample application and share it.

Regards,
Sudhir

sudhir kumar

unread,
Jan 14, 2017, 2:31:26 AM1/14/17
to vert.x
Hi @Clement /Julien ,

I have shared my sample test application :: https://github.com/sudhirsinha-github/SampleVertxTestApp .


Let me know if you need any further details. Looking forward for solution :)


Regards,
Sudhir 

Clement Escoffier

unread,
Jan 14, 2017, 10:03:28 AM1/14/17
to ve...@googlegroups.com
Hi,

I just had a very quick look.There are several issue in your MyServiceVerticle:

* remove the init method
* remove the vertx field, it’s stored in the AbstractVerticle class (that’s why you get null)

You should have something like:

public class MyServiceVerticle extends AbstractVerticle {
    private static final Logger logger = LoggerFactory.getLogger(MyServiceVerticle.class);

    private HttpServer httpServer;
    private Router router;

    private String getmongoUrl;

    @Override
    public void start() {
        getmongoUrl= config().getString("mongoBaseURL”); // Or vertx.getOrCreateContext().config().getString(“mongoBaseURL”);
        logger.info("Vertx is going to start @@@--->>>>" + getmongoUrl);
    }

}

Clement

sudhir kumar

unread,
Jan 14, 2017, 10:45:07 AM1/14/17
to vert.x
Hi,

Thanks for update. 
Still that not helped . I have done the changes and pushed the code.

Note : I have shared sample project to stimulate the given issue.

Regards,
Sudhir

Jez P

unread,
Jan 15, 2017, 3:05:37 AM1/15/17
to vert.x
Can you confirm how you are showing the problem? Are you running the test app? If so please provide the command line you are using to do so.

sudhir kumar

unread,
Jan 15, 2017, 4:42:49 AM1/15/17
to vert.x
Hi,

I am using intellij to run single test class.<<Attached screenshot>>



As whole ,i'm executing TC as "./gradlew clean build jacocoTestReport" for coverage reports.

Regards,
Sudhir
Screen Shot 2017-01-15 at 3.08.56 PM.png

Jez P

unread,
Jan 15, 2017, 6:37:37 AM1/15/17
to vert.x
I think your test is not running on any vertx context specifically (this might be different if you were using a vertx unit rule) so each time you call getOrCreateContext you are adding a new one, which has a different config. So you could try using the vertx unit rule (but even then remember the config isn't global on this context, you're passing it to your verticle, not your test). 

I added the following line in the start method of your verticle just before the logger line:-

System.out.println("TEST -- CONFIG VALUE SEARCH ****" +config().getString("mongoBaseURL"));

and I see the following output from the line in the verticle (which is starting on a specific context):-

TEST -- CONFIG VALUE SEARCH ****http://test.com

So my question is: what are you actually trying to test here? Why do you want to read the context's config in the test if you can establish that it's being passed to the verticle correctly once you set up the json object? 
Reply all
Reply to author
Forward
0 new messages