Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Junit tests in SpringBoot app are failing when using CommanLineRunner interface

There is a SpringBoot application, with SomeService and test class to test this service.

I can run my tests and everything works fine but as soon as I use CommandLineRunner interface ("implements CommandLineRunnner") in my main application class MyApplication then all my tests are starting to fail.

The problem is when I’m trying to get argument parameter in run method, like args[0] then my tests are starting to fail.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

and I’m getting this error:

Caused by: java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:774)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:755)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:319)

any idea how to fix it and run my tests without getting this error ?

my code looks like this:

@SpringBootApplication
public class MyApplication implements CommandLineRunner  {

    @Autowired
    SomeService someService;

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        String file = args[0];
        someService.calculate(file);
    }

}

@Component
public class SomeService {


    // some methods here...
}

@SpringBootTest
public class SomeServiceTest {

    @Autowired
    private SomeService someService;

    @Test
    public void some_test() {
        someService.someFunction();
        //....
    }
}

>Solution :

It is because CommandLineRunner#run() will be executed after spring boot start up . The input parameter args at here is normally passed from the command line that is used to run the spring boot app.

When you execute @SpringBootTest , it by default will have the empty argument list but now your method accesses its 0-index item by args[0] which will cause IndexOutOfBoundsException happens which in turn causes SpringApplication#callRunner() throws IllegalStateException

Suggest you to make your CommandLineRunner#run() to be null safe something like :

@Override
public void run(String... args) throws Exception {  
       if(args.length >= 1) {
            String file = args[0];
            someService.calculate(file);
       }
}

Or configure that argument list by :

@SpringBootTest(args= {"foo","bar","baz"})
public class FooTest {

}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading