将程序参数传递给 spring boot
passing program arguments to spring boot
我有一个 spring 启动批处理应用程序,它使用程序参数获取一些文件并对其进行操作。该应用程序运行良好,但我在 运行 junit 测试时遇到问题。
这是我的代码:
@Component
public class ApplicationArguments implements InitializingBean {
@Autowired private org.springframework.boot.ApplicationArguments appArgs;
private String filePath;
@Override
public void afterPropertiesSet() throws Exception {
filePath = appArgs.getSourceArgs()[0];
}
}
这个 bean 被另一个用来构建完整路径:
@Component
public class InitPaths implements InitializingBean {
@Autowired private ApplicationArguments myAppArgs;
private String fullPath;
@Override
public void afterPropertiesSet() throws Exception {
fullPath = myAppArgs.getFilePath(); //this will be null when launching tests
fullPath.toString();//this will throw a NullPointerException if we run the test
}
}
应用程序使用此命令运行良好:
java -jar myApp.jar fileName.txt
是否有任何解决方案可以将相同的参数传递给 junit 测试?
我尝试使用模拟,但遇到了同样的问题:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BatchTest {
@MockBean
ApplicationArguments applicationArguments;
@Autowired
@InjectMocks
InitPaths initPaths;
@Before
public void before() {
when(applicationArguments.getFilePath()).thenReturn("myCustomFile.dat");
}
@Test
public void contextLoad() {
}
}
这是错误:
Invocation of init method failed; nested exception is java.lang.NullPointerException
问题是因为 InitPaths
中的方法 afterPropertiesSet()
已经 运行 较早 before
在测试中。这意味着您的模拟 ApplicationArguments
没有任何模拟行为。从我的角度来看,您可能会创建具有预定义行为的新模拟 ApplicationArguments
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(ApplicationArgumentsTestConfig.class)
public class BatchTest {
@Autowired
InitPaths initPaths;
@Test
public void contextLoad() {
}
public static class ApplicationArgumentsTestConfig {
@Bean
@Primary
public ApplicationArguments mockArg() {
ApplicationArguments mocked = Mockito.mock(ApplicationArguments.class);
Mockito.when(mocked.getFilePath()).thenReturn("test-mock-path");
return mocked;
}
}
}
我刚刚帮我检查了作业。
我有一个 spring 启动批处理应用程序,它使用程序参数获取一些文件并对其进行操作。该应用程序运行良好,但我在 运行 junit 测试时遇到问题。 这是我的代码:
@Component
public class ApplicationArguments implements InitializingBean {
@Autowired private org.springframework.boot.ApplicationArguments appArgs;
private String filePath;
@Override
public void afterPropertiesSet() throws Exception {
filePath = appArgs.getSourceArgs()[0];
}
}
这个 bean 被另一个用来构建完整路径:
@Component
public class InitPaths implements InitializingBean {
@Autowired private ApplicationArguments myAppArgs;
private String fullPath;
@Override
public void afterPropertiesSet() throws Exception {
fullPath = myAppArgs.getFilePath(); //this will be null when launching tests
fullPath.toString();//this will throw a NullPointerException if we run the test
}
}
应用程序使用此命令运行良好:
java -jar myApp.jar fileName.txt
是否有任何解决方案可以将相同的参数传递给 junit 测试?
我尝试使用模拟,但遇到了同样的问题:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BatchTest {
@MockBean
ApplicationArguments applicationArguments;
@Autowired
@InjectMocks
InitPaths initPaths;
@Before
public void before() {
when(applicationArguments.getFilePath()).thenReturn("myCustomFile.dat");
}
@Test
public void contextLoad() {
}
}
这是错误:
Invocation of init method failed; nested exception is java.lang.NullPointerException
问题是因为 InitPaths
中的方法 afterPropertiesSet()
已经 运行 较早 before
在测试中。这意味着您的模拟 ApplicationArguments
没有任何模拟行为。从我的角度来看,您可能会创建具有预定义行为的新模拟 ApplicationArguments
@RunWith(SpringRunner.class)
@SpringBootTest
@Import(ApplicationArgumentsTestConfig.class)
public class BatchTest {
@Autowired
InitPaths initPaths;
@Test
public void contextLoad() {
}
public static class ApplicationArgumentsTestConfig {
@Bean
@Primary
public ApplicationArguments mockArg() {
ApplicationArguments mocked = Mockito.mock(ApplicationArguments.class);
Mockito.when(mocked.getFilePath()).thenReturn("test-mock-path");
return mocked;
}
}
}
我刚刚帮我检查了作业。