Quarkus的BeanManager与单元测试

huangapple go评论143阅读模式
英文:

Quarkus BeanManager with unit test

问题

BeanManager在上述用例中的单元测试中没有返回bean。该项目是一个Java库。

Bean接口

public interface My {
  String turn();
}

Bean限定符

@Qualifier
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface My1 {
}

Bean类

@ApplicationScoped
@My1
public class MyClass1 implements My {
    @Override
    public String turn() {
        return "1";
    }
}

以下单元测试的beans列表为空而失败。

@QuarkusTest
public class MyTest {

  @Test
  public void test1() throws IllegalAccessException, InstantiationException {
    Set<Bean<?>> beans = beanManager.getBeans(My.class, new AnnotationLiteral<My1>() {});
    MatcherAssert.assertThat(beans.isEmpty(), Is.is(false));
  }

  @Test
  public void test2() throws IllegalAccessException, InstantiationException {
    // Class<? extends Annotation>;
    final Class<? extends Annotation> annotationClass = My1.class;
    final Annotation qualifier = new Annotation() {

        @Override
        public Class<? extends Annotation> annotationType() {
            return annotationClass;
        }
    };
    Set<Bean<?>> beans = beanManager.getBeans(My.class, qualifier);
    MatcherAssert.assertThat(beans.isEmpty(), Is.is(false));
  }
}

test1和test2的JUnit输出

java.lang.AssertionError: 
Expected: is <false>
     but: was <true>
Expected :is <false>
Actual   :<true>

在另一个Java库项目中运行相同示例正常工作。

在单元测试类中添加一个注入的My属性也正常工作。

可能出了什么问题?
在这个例子中,BeanManager出了什么问题?

谢谢

英文:

BeanManager does not return beans during unit test in the use case describe above. The project is a Java library.

Bean interface

public interface My {
  String turn();
}

Bean qualifier

@Qualifier
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface My1 {
}

Bean class

@ApplicationScoped
@My1
public class MyClass1 implements My {
    @Override
    public String turn() {
        return &quot;1&quot;;
    }
}

The following unit test failed with empty beans list.

@QuarkusTest
public class MyTest {

  @Test
  public void test1() throws IllegalAccessException, InstantiationException {
    Set&lt;Bean&lt;?&gt;&gt; beans = beanManager.getBeans(My.class, new AnnotationLiteral&lt;My1&gt;() {});
    MatcherAssert.assertThat(beans.isEmpty(), Is.is(false));
  }


  @Test
  public void test2() throws IllegalAccessException, InstantiationException {
    // Class&lt;? extends Annotation&gt;
    final Class&lt;? extends Annotation&gt; annotationClass = My1.class;
    final Annotation qualifier = new Annotation() {

        @Override
        public Class&lt;? extends Annotation&gt; annotationType() {
            return annotationClass;
        }
    };
    Set&lt;Bean&lt;?&gt;&gt; beans = beanManager.getBeans(My.class, qualifier);
    MatcherAssert.assertThat(beans.isEmpty(), Is.is(false));
}

JUnit output for test1 and test2

java.lang.AssertionError: 
Expected: is &lt;false&gt;
     but: was &lt;true&gt;
Expected :is &lt;false&gt;
Actual   :&lt;true&gt;

Running the same sample in another Java library project works fine.

Adding an injected My attribute in the unit test class works fine too.

What could be wrong ?
What goes wrong with BeanManager in this example ?

Thx

答案1

得分: 3

很有可能您的 bean 被视为未使用,并在构建过程中被移除。详见 https://quarkus.io/guides/cdi-reference#remove_unused_beans 获取更多信息。

您可以尝试使用@Unremovable为您的 bean 添加注解。

另外请注意,BeanManager 不应该被应用程序代码使用。它是一个集成 SPI。此外,Quarkus 提供了更符合习惯的方式来集成第三方库和框架。

英文:

It is very likely that your bean is considered unused and removed during the build. See https://quarkus.io/guides/cdi-reference#remove_unused_beans for more info.

You can try to annotate your bean with @Unremovable.

Also note that BeanManager is not meant to be used by the application code. It's an integration SPI. Moreover, Quarkus offers more idiomatic ways of integrating third party libraries and framework.

huangapple
  • 本文由 发表于 2020年4月9日 04:00:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61109045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定