1

这是我的 REST 资源:

@Context
HttpServletRequest webRequest;


@Override
public DomainConfig get() {
    return configDelegate.get(webRequest.getHeader("Origin"));
}

我已经使用 Junit 5 + Weld / Mockito 扩展构建了我的单元测试。

@MockitoSettings(strictness = Strictness.STRICT_STUBS)
@ExtendWith(MockitoExtension.class)
@DisplayName("configs resource")
@EnableWeld
public class ConfigApiResourceTest {

    @Mock
    HttpServletRequest servletRequest;

    @WeldSetup
    public WeldInitiator weld = WeldInitiator
            .from(
                    MockCommonResources.class,
                    ConfigApiResource.class,
                    ConfigDelegate.class,
                    ConfigService.class,
                    etc etc
            )
            .addBeans(createHttpServletRequest())
            .activate(
                    RequestScoped.class,
                    ApplicationScoped.class
            )
            .build();

    Bean<?> createHttpServletRequest() {
        return MockBean.builder()
                .types(HttpServletRequest.class)
                .create(o -> servletRequest)
                .build();
    }

    @Test
    @DisplayName("config")
    void config(ConfigApiResource configApiResource) {

        final String url = "areaclient.infocert.it";

        when(servletRequest.getHeader("Origin")).thenReturn(url);

        final DomainConfig output = configApiResource.get();
        assertNotNull(output);

    }
}

问题是 HttpServletRequest webRequest 始终为空,可能是因为未注入但它是上下文对象。

所以真正的问题是,我怎样才能生成一个 HttpServletRequest 模拟并作为 @Context 对象注入?

4

2 回答 2

2

在向weld-junit组寻求帮助后,用户mkouba给出了最终解决方案。

final Weld weldBase = WeldInitiator.createWeld()
            .addBeanClasses(
                    MockCommonResources.class,
                    ConfigApiResource.class,
                    ConfigDelegate.class,
                    ConfigService.class,
                    etc etc
            )
            .addContainerLifecycleObserver(ContainerLifecycleObserver.processAnnotatedType()
                    .notify(pat -> pat.configureAnnotatedType()
                            .filterFields(m -> m.isAnnotationPresent(Context.class))
                            .forEach(m -> m.add(javax.enterprise.inject.literal.InjectLiteral.INSTANCE))));


    @WeldSetup
    public WeldInitiator weld = WeldInitiator
            .from(
                    weldBase
            )
            .activate(
                    RequestScoped.class,
                    ApplicationScoped.class
            )
            .addBeans(createHttpServletRequest())
            .build();

通过在Context注释上添加观察者, servletRequest模拟被正确注入到ConfigApiResource

于 2020-03-04T09:49:09.490 回答
1

我们在尝试@Inject private Boolean securityEnabled;对象时遇到了这个问题,因为不能模拟布尔值。我们使用自定义 JUnit/Mockito 扩展解决了这个问题:https ://github.com/exabrial/mockito-object-injection

 @InjectionMap
 private Map<String, Object> injectionMap = new HashMap<>();

 @BeforeEach
 public void beforeEach() throws Exception {
  injectionMap.put("securityEnabled", Boolean.TRUE);
 }

 @AfterEach
 public void afterEach() throws Exception {
  injectionMap.clear();
 }

你可以通过@MockingHttpServletRequest然后Context在注入映射中设置它们来做同样的事情。

于 2020-02-13T17:23:06.380 回答