We are building a command line data loader application at work that uses Spring. One of the things that I took us more time that it should have to figure out was how to write an integration test that invokes the command line application with the right command line arguments. This blog post describes this scenario and a potential solution to this problem.

Let’s consider a simple command line application implemented using Spring Boot’s CommandLineRunner. The main application class is fairly simple.

public class Application implements CommandLineRunner {
    DataService dataService;

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

    public void run(String... args) {

In addition to this, there is a configuration class AppConfiguration.

public class AppConfiguration {
    public DataService dataService() {
        return new DataService();

The DataService simply prints the argument it receives.

public class DataService {
    public void perform(String arg) {

If we were to attempt writing an integration test for this application, it would look like this:

@SpringApplicationConfiguration(classes = {Application.class})
public class ApplicationIntegrationTest {
    private Application application;

    public OutputCapture outputCapture = new OutputCapture();

    public void shouldGenerateResultFiles() throws Exception {

That test is straightforward—it loads the Spring context with all the beans, runs the application with parameters and expects the parameter to be printed to the console.

Except, it does not work. If we were to execute the above test, we will get the following error:

Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at in.sdqali.springapps.Application.run(Application.java:24) ~[main/:na]

What happened? It looks like the run method was called with out any arguments while the test configuration was loaded.

Let‘s try and understand wh y this happens. Remember how we annotated the test with @SpringApplicationConfiguration? This annotation is a meta-annotation 1 specified as:

@ContextConfiguration(loader = SpringApplicationContextLoader.class)
public @interface SpringApplicationConfiguration {

ContextConfiguration is an annotation that allows tests to specify the information used to load and configure the application context. From the Java doc for this annotation:

 * [email protected] @ContextConfiguration} defines class-level metadata that is used to determine
 * how to load and configure an [email protected] org.springframework.context.ApplicationContext
 * ApplicationContext} for integration tests.

This brings us to SpringApplicationContextLoader‘s loadContext method and buried deep inside are these line:

ConfigurableApplicationContext applicationContext = application.run();
return applicationContext;

So, regardless of how many arguments our command line application was supposed to take, this loader always call the application without any arguments.

There are two solutions I have been able to think of for this problem:

  • Override theSpringApplicationContextLoader and pass the necessary arguments in application.run(). This is definitely not an elegant or easy solution.
  • Use an environment variable instead of the command line argument as the input to the service. This will allow us to inject this variable using the @IntegrationTest annotation.

My next blog post will discuss the second approach.

  1. Meta annotations. Spring documentation. To see how to use meta-annotations to write custom annoations, see this blog post. [return]

If you have questions or comments about this blog post, you can get in touch with me on Twitter @sdqali.

If you liked this post, you'll also like...