I have a very basic Spring Boot application, which is expecting an argument from command line, and without it doesn\'t work. Here is the code.
@SpringBootApplica
I'm affraid that your solution will not work in a way that you presented (until you implement your own test framework for Spring).
This is because when you are running tests, Spring (its test SpringBootContextLoader
to be more specific) runs your application in its own way. It instantiates SpringApplication
and invokes its run
method without any arguments. It also never uses your main
method implemented in application.
However, you could refactor your application in a way that it'll be possible to test it.
I think (since you are using Spring) the easiest solution could be implemented using spring configuration properties instead of pure command line arguments. (But you should be aware that this solution should be used rather for "configuration arguments", because that's the main purpose of springs configuration properties
mechanism)
Reading parameters using @Value
annotation:
@SpringBootApplication
public class Application implements CommandLineRunner {
@Value("${myCustomArgs.customArg1}")
private String customArg1;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
Assert.notNull(customArg1);
//...
}
}
Sample test:
@RunWith(SpringRunner.class)
@SpringBootTest({"myCustomArgs.customArg1=testValue"})
public class CityApplicationTests {
@Test
public void contextLoads() {
}
}
And when running your command line app just add your custom params:
--myCustomArgs.customArg1=testValue