Integration testing challenges for non-web Spring applications

Fri, Dec 11, 2015

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) {, 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 {"sampleOutPut");

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 ~[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:

 * {@code @ContextConfiguration} defines class-level metadata that is used to determine
 * how to load and configure an {@link 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 =;
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 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]