I have some EJBs as JAX-WS Web Service:
@WebService
@Stateless
@Remote(MobileFacade.class)
public class MobileFacadeBean implements MobileFacade {
...
@Resource
WebServiceContext wsc;
...
}
Within this Web Service class, a WebServiceContext is injected via @Resource. I use this WebServiceContext to get the principal in the implementation. This works quite well, but now I wonder how to (Unit-)test this class!
So far, I was using OpenEJB to test my EJBs. Since the Web Service class also is an Stateless Session Bean, I would really like to use the same approach here. However, it does not work that easy – of course, it complains that there is no WebServiceContext when not called as a Web Service.
So the first question is: are there any ways to mock the WebServiceContext in OpenEJB?
And if no, what approach would you favour to test this kind of Web Service classes?
Cheers,
Frank
There are a handful of
@WebServiceunit test examples in the OpenEJB examples zip file. Everything you want should work fine.The webservice-security example sounds exactly like what you want. The version online uses
@RolesAllowedto make the container do the security check rather than doing it in code, but it is possible to check the principle in code. Here’s a slightly modified version of that example that worked for me with no issues.The bean
The Test
Libraries
If using maven, switch your normal
openejb-coredependency toopenejb-cxflike so. This will add Apache CXF and the OpenEJB/CXF integration code to your classpath.If not using maven, simplest approach is to just add all the jars from the
lib/directory of the OpenEJB zip file.