2

我有一些 EJB 作为 JAX-WS Web 服务:

@WebService
@Stateless
@Remote(MobileFacade.class)
public class MobileFacadeBean implements MobileFacade {
    ... 

   @Resource
   WebServiceContext wsc;

    ...
}

在这个 Web Service 类中,通过 @Resource 注入了一个 WebServiceContext。我使用这个 WebServiceContext 来获取实现中的主体。这工作得很好,但现在我想知道如何(单元)测试这个类!

到目前为止,我一直在使用 OpenEJB 来测试我的 EJB。由于 Web Service 类也是一个无状态会话 Bean,我真的很想在这里使用相同的方法。然而,它并没有那么容易工作——当然,它抱怨在不作为 Web 服务调用时没有 WebServiceContext。

所以第一个问题是:有什么方法可以在 OpenEJB 中模拟 WebServiceContext 吗?

如果不是,您更喜欢用什么方法来测试这种 Web 服务类?

干杯,弗兰克

4

2 回答 2

5

OpenEJB 示例 zip 文件中有一些@WebService单元测试示例。你想要的一切都应该可以正常工作。

webservice-security 示例听起来与您想要的完全一样。网上的版本@RolesAllowed是让容器做安全检查,而不是在代码里做,但是在代码里检查原理是可以的。这是该示例的略微修改版本,对我来说没有问题。

豆子

@DeclareRoles(value = {"Administrator"})
@Stateless
@WebService(
        portName = "CalculatorPort",
        serviceName = "CalculatorWsService",
        targetNamespace = "http://superbiz.org/wsdl",
        endpointInterface = "org.superbiz.calculator.CalculatorWs")
public class CalculatorImpl implements CalculatorWs, CalculatorRemote {

    @Resource
    private WebServiceContext webServiceContext;

    @RolesAllowed(value = {"Administrator"})
    public int sum(int add1, int add2) {
        // maybe log the principal or something -- prints "jane" in the test
        System.out.print(webServiceContext.getUserPrincipal());
        return add1 + add2;
    }

    @RolesAllowed(value = {"Administrator"})
    public int multiply(int mul1, int mul2) {
        return mul1 * mul2;
    }
}

考试

public class CalculatorTest extends TestCase {

    private InitialContext initialContext;

    protected void setUp() throws Exception {
        Properties properties = new Properties();
        properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
        properties.setProperty("openejb.embedded.remotable", "true");

        initialContext = new InitialContext(properties);
    }

    /**
     * Create a webservice client using wsdl url
     *
     * @throws Exception
     */
    public void testCalculatorViaWsInterface() throws Exception {
        URL url = new URL("http://127.0.0.1:4204/CalculatorImpl?wsdl");
        QName calcServiceQName = new QName("http://superbiz.org/wsdl", "CalculatorWsService");
        Service calcService = Service.create(url, calcServiceQName);
        assertNotNull(calcService);

        CalculatorWs calc = calcService.getPort(CalculatorWs.class);
        ((BindingProvider) calc).getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "jane");
        ((BindingProvider) calc).getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "waterfall");
        assertEquals(10, calc.sum(4, 6));
        assertEquals(12, calc.multiply(3, 4));
    }
}

图书馆

如果使用 maven,请将您的正常openejb-core依赖项切换为openejb-cxf这样。这会将 Apache CXF 和 OpenEJB/CXF 集成代码添加到您的类路径中。

<dependency>
  <groupId>org.apache.openejb</groupId>
  <artifactId>openejb-cxf</artifactId>
  <version>3.1.4</version>
  <scope>test</scope>
</dependency>

如果不使用 maven,最简单的方法是从lib/OpenEJB zip 文件的目录中添加所有 jar。

于 2011-06-01T16:39:32.677 回答
0

大卫,在您在 CalculatorTest 中的回答中,您使用了 CalculatorWs.class,它与 Web 服务端实现中使用的接口是否相同。我们必须创建 Web 服务客户端吗?

同样在大卫的例子中,而不是


QName calcServiceQName = new QName("http://superbiz.org/wsdl", "CalculatorWsService");

采用


QName calcServiceQName = new QName("http://superbiz.org/wsdl", "CalculatorPort");
于 2011-07-07T10:17:07.710 回答