【问题标题】:isolated testing of backing bean that uses RequestParam without jsf pages使用 RequestParam 没有 jsf 页面的支持 bean 的隔离测试
【发布时间】:2012-11-02 21:42:49
【问题描述】:

我想做一个比较简单的测试。正在测试的类(简化但你明白了):

@Named
@RequestScoped
public class SomeController {

    @Inject
    @RequestParam("someId")
    Long someId;

    public SomeClass getSomeClass() {

        return new SomeClass(someId);
    }
}

还有测试:

@RunWith(Arquillian.class)
public class SomeControllerTest {

    @Inject
    private SomeController controller;

    @Deployment
    public static Archive<?> createTestArchive() throws IOException {
    // trimmed out

    }

    @Test
    public void testNullGoalModelInjection() {
    //placeholder test so there are no errors during build

    // I am happy to use setter injection and do
    controller.setSomeId(1);

    // even better if I could get that injected in too
    assertNotNull(controller.getSomeClass());
    }

}

在尝试中,我得到了异常:

java.lang.IllegalStateException: Attempted to inject an HttpServletRequest before it has been initialized.

这是有道理的。

我真正想知道的是:

有没有办法通过 Arquillian(或其他)测试这样的 bean,而不涉及创建 jsf 页面,然后使用 jsfunit/warp/或其他一些机制来调用 http 请求?

换句话说,我如何通过测试调用一个 http 请求,这将为我生成这个 bean - 但不需要 jsf 文件等就位。

感谢任何帮助/建议。

【问题讨论】:

  • 你在哪个容器上运行这个?

标签: seam3 jboss-arquillian


【解决方案1】:

JBossAS7 使用通过 JMX 调用测试的 Arquillian 协议,因此根本没有可用的 HttpRequest。

尝试更改与 JBossAS7 交互时使用的协议,如下所述: https://community.jboss.org/message/722871#722871

现在测试通过 HTTP 调用到 servlet,您的测试应该运行。

【讨论】:

  • 谢谢。在我看到你的答案的时间里,我想出了一种替代方法(不是双关语)。我为RequestParamProducer 设置了一个@Alternative @Specializes 生产者,并将其定义为beans.xml,以便在测试期间激活。这可行,但您的建议提供了一个零耦合的更清洁的解决方案。但是,@Alternative 选项确实有一个好处,即我可以使用私有字段来注入特定值并且不需要设置器。
猜你喜欢
  • 2018-04-04
  • 1970-01-01
  • 2011-09-01
  • 2016-10-26
  • 2011-04-07
  • 2011-10-13
  • 2014-02-25
  • 1970-01-01
  • 2011-02-10
相关资源
最近更新 更多