2 Replies Latest reply on Oct 29, 2008 5:30 PM by Olivier Thierry

    Authentication and unit tests

    Olivier Thierry Apprentice


      I try to write a unit test for a Seam component in which I inject org.jboss.seam.security.identity component. Actually this component uses identity.getCredentials().getUserName() method. But I can't see an easy way to authenticate when running unit tests. I mean I don't want to have to setup an authentication manager or something like that, I just need to have identity component instantiated with credentials set. How should I do this ?

      Thanks in advance,


        • 1. Re: Authentication and unit tests
          Olivier Thierry Apprentice

          I still try to make it work but I can't find the way to do it. Drawing on org.jboss.seam.test.integration.security.SecurityTest, I wrote a mock identity component which uses MockLoginModule bundled with Seam.

          @Install(precedence = Install.MOCK)
          public class MockIdentity extends Identity {
              private Configuration createMockJAASConfiguration() {
                  return new Configuration() {
                   private AppConfigurationEntry[] aces = { new AppConfigurationEntry(
                    new HashMap<String, String>()) };
                   public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
                       return aces;
                   public void refresh() {
              protected LoginContext getLoginContext() throws LoginException {
                  return new LoginContext("default", getSubject(), getCredentials()
                   .createCallbackHandler(), createMockJAASConfiguration());

          Then I wrote my test method  :

          public void testSaveThenDeleteEmployee()  throws java.lang.Exception
              new ComponentTest() {
               public void testComponents() throws java.lang.Exception {
                   EmployeeInternalServiceLocal service = (EmployeeInternalServiceLocal) getInstance("employeeInternalService");
                       // Login
                   Identity identity = (Identity) getInstance("org.jboss.seam.security.identity");
                   assert "loggedIn".equals(identity.login());
                   assert identity.isLoggedIn();
                   // Create an employee
                   Employee employee = new Employee();
                   employee = service.saveEmployee(employee);
                   assert employee.getId() != null;
                   assert employee.getCreationDate() != null;
                   assert employee.getCreationUser() != null;
                   assert employee.getCreationDate().equals(employee.getLastUpdateDate());
                   assert employee.getCreationUser().equals(employee.getLastUpdateUser());

          In my employeeInternalService component, I inject the identity component :

          org.jboss.seam.security.Identity identity;

          And finally I use it this way :

          java.util.Date lastUpdateDate = new java.util.Date(); 
          if (identity != null && identity.getPrincipal() != null) {

          But I still have null value in creationUser and lastUpdateUser fields. Yet it should work with a mock identity component, doesn't it ? Did I miss something ?

          • 2. Re: Authentication and unit tests
            Olivier Thierry Apprentice

            How stupid I am ! I tried something complicated while it was so simple. I had no need of MockLoginModule, I just had to use standard SeamLoginModule and write my own very simple authenticator :

            public class MockAuthenticator {
                 public boolean authenticate() {
                      return true;

            Then in components.xml used for my tests :

            <security:identity authenticate-method="#{authenticator.authenticate}" />

            And in my test method :

            Identity identity = Identity.instance();
            assert "loggedIn".equals(identity.login());

            Actually you just have to do it the standard way, no special need for unit tests ! And it took hours for me to see something so obvious lol