Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:51:33+00:00 2026-05-22T20:51:33+00:00

I have some EJBs as JAX-WS Web Service: @WebService @Stateless @Remote(MobileFacade.class) public class MobileFacadeBean

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:51:34+00:00Added an answer on May 22, 2026 at 8:51 pm

    There are a handful of @WebService unit test examples in the OpenEJB examples zip file. Everything you want should work fine.

    • simple-webservice
    • webservice-attachments
    • webservice-security
    • webservice-ws-security

    The webservice-security example sounds exactly like what you want. The version online uses @RolesAllowed to 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

    @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;
        }
    }
    

    The Test

    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));
        }
    }
    

    Libraries

    If using maven, switch your normal openejb-core dependency to openejb-cxf like so. This will add Apache CXF and the OpenEJB/CXF integration code to your classpath.

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

    If not using maven, simplest approach is to just add all the jars from the lib/ directory of the OpenEJB zip file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two EJBs A and B: public class A implements AInterface
I have a Java Web Start application calling remote Java EE 5 EJBs to
I have an EJB3 stateless session bean that uses some other ejbs to do
I have some EJBs that use Hibernate to persist data to the database. I
I have a JavaEE6 application, consisting of Web stuff and EJBs and which is
I have a Java EE web application that does not make use of EJBs.
I want to do something like this: @Stateless public class GreeterEjb { private final
We have some staleless EJBs (EJB3) deployed on a GlassFish 2 server that expose
I am having big problems in fetching exception details from Remote EJBs... I have
Let's say I have an interceptor that looks smth like this: public class AuthorizationInterceptor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.