Total Pageviews

2020/01/07

[Spring] How to pass command line argument to Spring Boot application via junit ?

Here has sample code:
package com.test.batch;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class BatchApplicationTests {

 @Autowired
 private ApplicationContext ctx;

 @Test
 public void testCommandline() throws Exception {
  CommandLineRunner runner = ctx.getBean(CommandLineRunner.class);
  runner.run("-name", "Job2");
 }

}

No comments: