简体   繁体   English

在 QuarkusTestCallback 中使用 ConfigProperties

[英]Use ConfigProperties in a QuarkusTestCallback

For my integration tests, I would like to simplify the oauth security with an annotation and a QuarkusTestBeforeTestExecutionCallback , that sets up my RestAssured instance with the necessary oauth access token for a user with desired roles.对于我的集成测试,我想通过注释和QuarkusTestBeforeTestExecutionCallback简化 oauth 安全性,为具有所需角色的用户设置具有必要 oauth 访问令牌的 RestAssured 实例。

Everything would work just fine, the single problem I'm facing is, that I do not get the URL of the Keycloak Devservice into my QuarkusTestBeforeTestExecutionCallback , as apparently @ConfigProperty(name = "quarkus.oidc.auth-server-url") , which does work in the @QuarkusTest does not work in the callback class.一切都会正常工作,我面临的唯一问题是,我没有将 Keycloak Devservice 的 URL 放入我的QuarkusTestBeforeTestExecutionCallback中,显然是@ConfigProperty(name = "quarkus.oidc.auth-server-url") ,在@QuarkusTest中起作用的在回调 class 中不起作用。 As the callback class is somehow executed in the context of the @QuarkusTest , the property should be theoretically somehow accessible I guess.由于回调 class 以某种方式在@QuarkusTest的上下文中执行,因此我猜该属性理论上应该可以以某种方式访问。

How can I get a config property, which is available in a @QuarkusTest in a QuarkusCallback class?如何获取配置属性,该属性在 QuarkusCallback class 的@QuarkusTest中可用?

I managed to get the desired ConfigProperty with manual invocation using the classloader of the test object, as following:我设法使用测试 object 的类加载器通过手动调用获得所需的 ConfigProperty,如下所示:

public class MyCallback implements QuarkusTestBeforeEachCallback {
    @Override
    public void beforeEach(QuarkusTestMethodContext context) {
        ClassLoader classloader = context.getTestInstance().getClass().getClassLoader();
        Config config = ConfigProvider.getConfig(classLoader);
        ConfigValue configValue = config.getConfigValue("my.config.property.name");
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM