集成测试EJB和REST资源的最佳方法是什么?
答案 0 :(得分:1)
您可以通过EJB 3.1 EJBContainer API以及支持EJB lite以外的容器来实现。 EJB lite应该是最小值而不是最大值。
我知道OpenEJB支持JAX-RS嵌入式,而不是其他人。如果他们通过嵌入式Arquillian适配器支持JAX-RS,他们应该能够通过标准EJBContainer API支持它,该API用于Java SE用途,如测试或独立应用程序。
以下是一个例子:
那个没有详细记录,所以这里是亮点。
首先,包括嵌入式容器库。在这种情况下,这是以下maven依赖:
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-cxf-rs</artifactId>
<version>4.0.0-beta-3-SNAPSHOT</version>
<scope>test</scope>
</dependency>
然后在你的测试用例中,启动EJBContainer:
final Properties properties = new Properties();
properties.setProperty("openejb.embedded.remotable", "true");
EJBContainer.createEJBContainer(properties);
您很高兴,现在您可以向您的服务发送请求了。使用CXF WebClient,可能如下所示:
Response response = WebClient.create("http://localhost:4204/rest-on-ejb")
.path("/user/create")
.query("name", "dummy")
.query("pwd", "unbreakable")
.query("mail", "foo@bar.fr")
.put(null);
// Then check the results via invoking the bean directly
List<User> list = service.list(0, 100);
for (User u : list) {
if (!users.contains(u)) {
service.delete(u.getId());
return;
}
}
fail("user was not added");
当然普通URL或Commons HttpClient也可以。
答案 1 :(得分:0)
EJB - Arquillian
REST - Rest Assured