0

I have a method that is shown as below and this in turn call multiple private methods, that I won't be posting here.

@Bean
    public CommandLineRunner registerStartersAndReaders(final Vertx vertx, final SpringVerticleFactory springVerticleFactory,
                                                        final SpringUtil springUtil, final GslConfig gslConfig) {
        return args -> {
            // Scan all the beans annotated with the @ElasticsearchBatchDataListener annotation.
            List<Pair<Object, Method>> listenerMethods = springUtil.getListenerMethods();

            // Deploy the starters per listener.
            deployVerticle(listenerMethods, jsonConfig -> deployStarterVerticle(vertx, springVerticleFactory, jsonConfig), config);

            // Deploy the reader verticles.
            deployVerticle(listenerMethods, jsonConfig -> deployReaderVerticle(vertx, springVerticleFactory, jsonConfig), config);
            setupTriggers(vertx, listenerMethods, config);
        };
    }

Then I have a test method for it :

@Test
    public void registerStartersAndReadersTest() {
        when(springUtil.getListenerMethods()).thenReturn(value);
        CommandLineRunner runner = config.registerStartersAndReaders(vertx, springVerticleFactory, springUtil, config);
        assertNotNull(runner);
    }

Here, all the parameters passed into the method call are mocks. The problem is, when I run this test, it passes but it returns the value without getting into the private methods as it just returns 'args'.

Can someone please guide me, as to how I can make my test cover all the possible code. I am not supposed to change my code for the test.

1 Answer 1

2

I think you got confused with the lamba expression, and believe me it is very confusing in the beginning. But once you are fluent with it, it will be a breeze.

So here you got the instance of CommandLineRunner from method registerStartersAndReaders call, and your assertNotNull PASS as you have the not null instance, but until you call the run method of FunctionalInterface nothing will be executed.

Add runner.run(args) to execute the method(s) in your test case.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much for the answer. I really got confused as this is the first time I am working with these expressions. But adding the runner.run(args) code, has solved multiple issues. :)
Glad I was able to help. Welcome to Stack Overflow. Please note that the preferred way of saying 'thanks' around here is by up-voting good questions and helpful answers (once you have enough reputation to do so), and by accepting the most helpful answer to any question you ask (which also gives you a small boost to your reputation). Please see the About page and also How do I ask questions here? & [What should I do when someone answers my question?] (stackoverflow.com/help/someone-answers)
@J.Akansha - As the asker, you have a special privilege: you may accept the answer that you believe is the best solution to your problem. You may change which answer is accepted, or simply un-accept the answer, at any time. Accepting an answer is not mandatory; do not feel compelled to accept the first answer you receive. Wait until you receive an answer that answers your question well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.