Test your business logic with JUnit

Contains useful source code for specific use cases.

Test your business logic with JUnit

Postby Development@SIB » Sun Mar 20, 2011 3:34 pm

Tests our business logic without an application server, but with our Lifecycle objects. We test our server code without specific configuration or modifications for unit tests.

Business Object:

Code: Select all
public class UserRegistration
{
   /**
    * Removes/Unregisters a user and all its content.
    *
    * @param pUserName the user name
    * @throws Exception if the user can not be deleted
    */
   public void delete(String pUserName) throws Exception
   {
      try
      {
         DBAccess dba = (DBAccess)SessionContext.getCurrentSession().get("newDBAccess");
         
         DBStorage dbsUser = new DBStorage();
         dbsUser.setDBAccess(dba);
         dbsUser.setWritebackTable("USERS");
         dbsUser.open();
         
         List<IBean> liBeans = dbsUser.fetchBean(new Equals("USERNAME",
                                                            pUserName), null, 0, -1);
         
         //the username is unique and it's not possible that a user exists
         //more than once!
         if (liBeans.size() == 1)
         {
            //delete the user (db will cascade all user-data)
            dbsUser.delete(liBeans.get(0));
         }
         else
         {
            throw new SecurityException("User '" + pUserName + "' was not found!");
         }
      }
      catch (Throwable th)
      {
         throw new SecurityException("It's not possible to delete the user!", th);
      }
   }
}   // UserRegistration

A standard Lifecycle Object:

Code: Select all
public class Session extends GenericBean
{
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   // User-defined methods
   //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

   /**
    * Creates a new database access object.
    *
    * @return the new database access object
    * @throws Exception if the connection can not be opened
    */
   public DBAccess getNewDBAccess() throws Exception
   {
      DBAccess dba = DBAccess.getDBAccess(DBSecurityManager.getCredentials(
                                          SessionContext.getCurrentSessionConfig()));
      dba.open();
      
      return dba;
   }
   
   /**
    * Returns the access to the database.
    *
    * @return the access to the database
    * @throws Exception if the datasource can not be opened
    */
   public DBAccess getDBAccess() throws Exception
   {
      DBAccess dba = (DBAccess)get("dBAccess");
      
      if (dba == null)
      {
         dba = getNewDBAccess();
               
         put("dBAccess", dba);
      }
      
      return dba;
   }

   /**
    * Gets the user registration business object.
    *
    * @return the business object for user registrations
    */
   public UserRegistration getRegistration()
   {
      UserRegistration ureg = (UserRegistration)get("registration");
      
      if (ureg == null)
      {
         ureg = new UserRegistration();
         
         put("registration", ureg);
      }
      
      return ureg;
   }

}    // Session

Unit Test:

Code: Select all
/**
 * Tests the our delete method from the user registration object.
 *
 * @throws Throwable if the test fails
 */
@Test
public void testDelete() throws Throwable
{
   AbstractConnection con = createConnection(null);
   
   try
   {
      //delete user with the name "unknownuser"
      con.call("registration", "delete", "unknownuser");
      
      Assert.fail("User 'unknownuser' found!");
   }
   catch (SecurityException se)
   {
      Assert.assertEquals("User 'unknownuser' was not found!",
                          se.getMessage());
   }

   con.close();
}

/**
 * Creates a new connection.
 *
 * @param pConProps additional connection properties
 * @return the connection
 * @throws Throwable if the connection can not be opened
 */
private AbstractConnection createConnection(Hashtable<String, String> pConProps) throws Throwable
{
   MasterConnection macon = new MasterConnection(new DirectServerConnection());
   macon.setApplicationName("app");
   macon.setUserName("user");
   macon.setPassword("pwd");
   
   if (pConProps != null)
   {
      for (Map.Entry<String, String> entry : pConProps.entrySet())
      {
         macon.setProperty(entry.getKey(), entry.getValue());
      }
   }
   
   macon.open();
   
   return macon;
}
User avatar
Development@SIB
 
Posts: 311
Joined: Mon Sep 28, 2009 1:54 pm

Return to Code snippets