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
In your code autowire springs ApplicationArguments. Use getSourceArgs() to retrieve the commandline arguments.
public CityApplicationService(ApplicationArguments args, Writer writer){
public void writeFirstArg(){
writer.write(args.getSourceArgs()[0]);
}
}
In your test mock the ApplicationArguments.
@RunWith(SpringRunner.class)
@SpringBootTest
public class CityApplicationTests {
@MockBean
private ApplicationArguments args;
@Test
public void contextLoads() {
// given
Mockito.when(args.getSourceArgs()).thenReturn(new String[]{"Berlin"});
// when
ctx.getBean(CityApplicationService.class).writeFirstArg();
// then
Mockito.verify(writer).write(Matchers.eq("Berlin"));
}
}
Like Maciej Marczuk suggested, I also prefer to use Springs Environment properties instead of commandline arguments. But if you cannot use the springs syntax --argument=value you could write an own PropertySource, fill it with your commandline arguments syntax and add it to the ConfigurableEnvironment. Then all your classes only need to use springs Environment properties.
E.g.
public class ArgsPropertySource extends PropertySource
BTW:
Since I do not have enough reputation points to comment an answer, I would still like to leave a hard learned lesson here:
The CommandlineRunner is not such a good alternative. Since its run() method alwyas gets executed right after the creation of the spring context. Even in a test-class. So it will run, before your Test started ...