junit5扩展中的Quarkustest访问豆

发布于 2025-02-04 15:58:11 字数 690 浏览 5 评论 0原文

我有@quarkustest基于测试类。我想实现与我的quarkus测试上下文的特定豆类交互的Junit 5扩展名(eachCallback,aftereachCallback)。我尝试了cdi.current(),但结果为:java.lang.illegalstateexception:无法

在春季基于春季的测试中找到CDIPROVIDE ,例如,我访问IpplicationContext

@Override
  public void beforeEach(final ExtensionContext extensionContext) {
    final ApplicationContext applicationContext = SpringExtension.getApplicationContext(extensionContext);
    MyBean myBean = applicationContext.getBean(MyBean.class);
}

,然后可以从我的测试上下文中使用编程性查询混凝土豆。有什么类似的夸克测试方法吗?我的意思是,我可以@inject将bean进入我的测试类,并以@beforeach方法访问它,但是我正在寻找一种更“可重复使用的”解决方案。

非常感谢。

I have @QuarkusTest based test class. And I want to implement a JUnit 5 extension (BeforeEachCallback, AfterEachCallback) that interacts with a specific bean of my Quarkus test context. I tried CDI.current(), but that results into: java.lang.IllegalStateException: Unable to locate CDIProvide

In Spring based test for example I access the ApplicationContext via

@Override
  public void beforeEach(final ExtensionContext extensionContext) {
    final ApplicationContext applicationContext = SpringExtension.getApplicationContext(extensionContext);
    MyBean myBean = applicationContext.getBean(MyBean.class);
}

which I can then use to programmatically query concrete beans from my test context. Is there any kind of similar approach to Quarkus tests? I mean, I can @Inject the bean into my test class and access it in a @BeforeEach method, but I am looking for a more 'reusable' solution.

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

2025-02-11 15:58:11

geoand 让我走上正确的轨道。一种有效的方法是使用 quarkusteStepeStepEfferrer“

因此,鉴于我的自定义QuarkusteStbeforeachCallback实现了现在,该实现现在支持通过CDI访问Quarkus bean:

public class MyBeforeEachCallback implements QuarkusTestBeforeEachCallback {

    @Override
    public void beforeEach(QuarkusTestMethodContext context) {
        if (Stream.of(context.getTestInstance().getClass().getAnnotations()).anyMatch(DoSomethingBeforeEach.class::isInstance)) {
            final Object myBean = CDI.current().select(MyBean.class)).get()
            // ... do something with myBean
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface DoSomethingBeforeEach {
        // ...
    }
}   

和服务声明文件

src/test/test/resources/Meta-inf/services/io.quarkus.junit.junit.callback.quarkustestepestepefte.quarkustestebock.quarkusteceachcalforeachcallbackcallback

使用内容

com.something.test.mybeforeachcallback

我现在可以在我的@quarkustest测试中使用它,例如:

@QuarkusTest
@DoSomethingBeforeEach
public abstract class AbstractV2Test {

// ...

}

这比我们用Spring所用的要复杂一些引导测试,但肯定有效。

geoand put me on the right track. A valid approach would be to use QuarkusTestBeforeEachCallback / QuarkusTestMethodContext.

So given my custom QuarkusTestBeforeEachCallback implementation which now supports accessing quarkus beans via CDI:

public class MyBeforeEachCallback implements QuarkusTestBeforeEachCallback {

    @Override
    public void beforeEach(QuarkusTestMethodContext context) {
        if (Stream.of(context.getTestInstance().getClass().getAnnotations()).anyMatch(DoSomethingBeforeEach.class::isInstance)) {
            final Object myBean = CDI.current().select(MyBean.class)).get()
            // ... do something with myBean
        }
    }

    @Retention(RetentionPolicy.RUNTIME)
    public @interface DoSomethingBeforeEach {
        // ...
    }
}   

and a service declaration file

src/test/resources/META-INF/services/io.quarkus.test.junit.callback.QuarkusTestBeforeEachCallback

with the content

com.something.test.MyBeforeEachCallback

I now can use it in my @QuarkusTest tests e.g:

@QuarkusTest
@DoSomethingBeforeEach
public abstract class AbstractV2Test {

// ...

}

This is a bit more complicated than what we are used to with Spring Boot tests, but it definitely works.

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