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
As mentioned in this answer, Spring Boot currently doesn't offer a way to intercept/replace the DefaultApplicationArguments that it uses. A natural-Boot-way that I used to solve this was to enhance my runner logic and use some autowired properties.
First, I created a properties component:
@ConfigurationProperties("app") @Component @Data
public class AppProperties {
boolean failOnEmptyFileList = true;
boolean exitWhenFinished = true;
}
...autowired the properties component into my runner:
@Service
public class Loader implements ApplicationRunner {
private AppProperties properties;
@Autowired
public Loader(AppProperties properties) {
this.properties = properties;
}
...
...and, in the run
I only assert'ed when that property is enabled, which is defaulted to true
for normal application usage:
@Override
public void run(ApplicationArguments args) throws Exception {
if (properties.isFailOnEmptyFileList()) {
Assert.notEmpty(args.getNonOptionArgs(), "Pass at least one filename on the command line");
}
// ...do some loading of files and such
if (properties.isExitWhenFinished()) {
System.exit(0);
}
}
With that, I can tweak those properties to execute in a unit test friendly manner:
@RunWith(SpringRunner.class)
@SpringBootTest(properties = {
"app.failOnEmptyFileList=false",
"app.exitWhenFinished=false"
})
public class InconsistentJsonApplicationTests {
@Test
public void contextLoads() {
}
}
I needed the exitWhenFinished
part since my particular runner normally calls System.exit(0)
and exiting that way leaves the unit test in a semi-failed state.