试图让Spring (DataSource)自动进入JUnit ParameterResolver。但到了春天,DataSource还没有被注射。
我已经注册了SpringExtension,并提供了上下文位置(aaspire-test-datasource-components.xml)来加载ApplicationContext。
@ContextConfiguration(locations={"classpath:spring-config/aaspire-test-datasource-
components.xml"})
@ExtendWith(SpringExtension.class)
public class gdnContextResolver implements ParameterResolver
{
@Autowired
private DataSource dataSource;
@Override
public boolean supportsParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.getParameter().getType() == gdnContext.class;
}
@Override
public Object resolveParameter(ParameterContext parameterContext,
ExtensionContext extensionContext) throws ParameterResolutionException {
try {
return SpringBatchJobUtil.createJobExecutionGdnContext(dataSource);
} catch (Exception e) {
throw new ParameterResolutionException(ExceptionUtil.getMessageText(e));
}
}
}发布于 2022-05-30 10:26:39
您不能通过@ExtendWith注册扩展名。
此外,Spring注释(如@ContextConfiguration )不能应用于扩展,也不能将Spring组件自动添加到扩展中。
因此,您需要删除@ExtendWith和@ContextConfiguration声明,而不是将DataSource自动加载到扩展中的字段中,您需要使用SpringExtension来检索“当前测试”的ApplicationContext。
您可以在您的resolveParameter()实现中实现后者,如下所示。
DataSource dataSource = SpringExtension.getApplicationContext(extensionContext).getBean(DataSource.class);https://stackoverflow.com/questions/71866678
复制相似问题