title: OpenEJB & JUnit: Sessioncontext.isCallerInRole returns allways false
body: <p>I need to write a session bean that somewhere in the code checks if the current user has some role(s). </p>
  
  <p>To unittest my EJB3 I'm trying out OpenEJB. I followed their example about <a href="http://openejb.apache.org/3.0/testing-security-example.html" rel="nofollow" title="testing security">testing security</a> but if I test in my code for the role with SessionContect.isCallerInRole() it always returns false.</p>
  
  <p><strong>Why doesn't it work?</strong> </p>
  
  <p>I've written some code to illustrate.</p>
  
  <p>My local interface:</p>
  
  <pre><code>@Local
  public interface MyBean {
  
      boolean doSomething();
  
  }
  </code></pre>
  
  <p>My EJB:</p>
  
  <pre><code>@Stateless
  public class MyBeanImpl implements MyBean {
  
      @Resource
      private SessionContext sessionContext;
  
      @Override
      public boolean doSomething() {
          return this.sessionContext.isCallerInRole("role1");
      }
  
  }
  </code></pre>
  
  <p>My test:</p>
  
  <pre><code>public class MyBeanTest {
  
      private Context context;
  
      @Before
      public void setUp() throws Exception {
          final Properties properties = new Properties();
          properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.LocalInitialContextFactory");
  
          this.context = new InitialContext(properties);
      }
  
      @Test
      public void test1() throws Exception {
          final Caller roleBean = (Caller) this.context.lookup("RoleBeanLocal");
          roleBean.call(new Callable&lt;Object&gt;() {
  
              @Override
              public Object call() throws Exception {
                  final MyBean myBean = (MyBean) MyBeanTest.this.context.lookup("MyBeanImplLocal");
                  Assert.assertTrue(myBean.doSomething());
                  return null;
              }
          });
      }
  
      @Test
      public void test2() throws Exception {
          final Caller role2Bean = (Caller) this.context.lookup("Role2BeanLocal");
          role2Bean.call(new Callable&lt;Object&gt;() {
  
              @Override
              public Object call() throws Exception {
                  final MyBean myBean = (MyBean) MyBeanTest.this.context.lookup("MyBeanImplLocal");
                  Assert.assertFalse(myBean.doSomething());
                  return null;
              }
          });
      }
  
      public static interface Caller {
  
          &lt;V&gt; V call(Callable&lt;V&gt; callable) throws Exception;
  
      }
  
      @Stateless
      @RunAs("role1")
      public static class RoleBean implements Caller {
  
          @Override
          public &lt;V&gt; V call(final Callable&lt;V&gt; callable) throws Exception {
              return callable.call();
          }
  
      }
  
      @Stateless
      @RunAs("role2")
      public static class Role2Bean implements Caller {
  
          @Override
          public &lt;V&gt; V call(final Callable&lt;V&gt; callable) throws Exception {
              return callable.call();
          }
  
      }
  }
  </code></pre>