在Junit Jupiter中注入弹簧豆进入参数
尝试将Spring组件(dataSource
)自动化为Junit parameterresolver
。但是dataSource
没有在春季注射。
我已经注册了SpringExtension
,还提供了上下文位置(aaspire-test-tatasource-components.xml
),以加载application> application> application> 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));
}
}
}
Trying to get Spring component (DataSource
) autowired into JUnit ParameterResolver
. But DataSource
is not getting injected by Spring.
I have registered the SpringExtension
and also provided the context location (aaspire-test-datasource-components.xml
) to load the 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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法通过
@ExtendWith
在扩展上注册扩展名。此外,诸如
@ContextConfiguration
之类的弹簧注释不能应用于扩展名,也不能将Spring组件自动化为扩展名。因此,您需要将
@extendwith
和@contextConfiguration
声明删除,而不是自动将dataSource
自动化到您需要的领域中使用SpringExtension
来检索“当前测试”的ApplicationContext
。您可以在
ResolveParameter()
实现中实现后者,如下所示。You cannot register extensions on an extension via
@ExtendWith
.In addition, Spring annotations such as
@ContextConfiguration
cannot be applied to an extension, and you cannot autowire Spring components into an extension.So, you need to remove the
@ExtendWith
and@ContextConfiguration
declarations, and instead of autowiring theDataSource
into a field in your extension you need to use theSpringExtension
to retrieve theApplicationContext
for the "current test".You can achieve the latter in your
resolveParameter()
implementation as follows.