在Junit Jupiter中注入弹簧豆进入参数

发布于 2025-01-21 16:55:07 字数 1253 浏览 7 评论 0原文

尝试将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2025-01-28 16:55:07

您无法通过@ExtendWith在扩展上注册扩展名。

此外,诸如@ContextConfiguration之类的弹簧注释不能应用于扩展名,也不能将Spring组件自动化为扩展名。

因此,您需要将@extendwith@contextConfiguration声明删除,而不是自动将dataSource自动化到您需要的领域中使用SpringExtension来检索“当前测试”的ApplicationContext

您可以在ResolveParameter()实现中实现后者,如下所示。

DataSource dataSource = SpringExtension.getApplicationContext(extensionContext).getBean(DataSource.class);

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 the DataSource into a field in your extension you need to use the SpringExtension to retrieve the ApplicationContext for the "current test".

You can achieve the latter in your resolveParameter() implementation as follows.

DataSource dataSource = SpringExtension.getApplicationContext(extensionContext).getBean(DataSource.class);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文