Showing posts with label testing. Show all posts
Showing posts with label testing. Show all posts

Wednesday, April 09, 2008

Unit Testing Loaders

To unit test a loader, you first need to extend it so that the load method is exposed.  I usually just create a private static final class within the test class that I am using to test the loader.

private static final class ExposedFoodEvidenceLoader extends FoodEvidenceLoader {
    public void load(final RulesParameters rp) throws AppException, InformationalException {
        super.load(rp);
    }
}

In the actual test method, I just create the RDO instances and execute the loader.

final FoodEvidenceGroup foodEvidenceGroup = new FoodEvidenceGroup();
final ItemGroupGlobals itemGroupGlobals = new ItemGroupGlobals();
itemGroupGlobals.dateOfCalculation.setValue(Date.getCurrentDate()); 

final RulesParameters rulesParameters = new RulesParameters();
rulesParameters.addRDO(itemGroupGlobals, false);
rulesParameters.addRDO(foodEvidenceGroup, false);

new ExposedFoodEvidenceLoader().load(rulesParameters);

assertEquals(FOODTYPE.FILIPINO, foodEvidenceGroup.getfoodType().getValueNoLoad());

Wednesday, October 24, 2007

JUnit Testing in Curam

Curam out of the box comes with some rudimentary JUnit testing capability within their EJBServer project since around version 3.  I've been using it instead of something I wrote up back in 2004 for my unit testing because at least it comes with Curam.  However, my colleagues have told me that there have been no instructions on setting it up till now.  So, this is my attempt at documenting what you need to do in order to get your Curam unit tests working.

Note: for those I have talked with on this subject before, this is a simpler way of doing things rather than what I have been doing in my previous projects.

Setting up a place for your unit tests

The first thing you would need to do is find a place to put your tests.

  1. Make a new source folder EJBServer\components\custom\tests.  The last path element has to be tests.
  2. When I initially set things up, I always create a test package called curam.yourproject.test.sanity.  In this folder I create a simple unit tests to prove that things work correctly.  In which case I put in the following test and verify that it runs within Eclipse.
public SanityTest extends TestCase {
  public void testSanity() throws Exception {
    assertTrue(true);
  }
}

Verifying that your unit test works in Curam

The next thing you need to deal with is running the actual test within Curam's build scripts.  To do this you just have to type the following in the command prompt:

build supplement -Dsupplement=tests
build test

The supplement target creates a supplementary jar and will pull any sources that have the value of supplement in their source path.  In this case it would be tests.

The test target runs the test cases and generates an HTML report.  The report is located in EJBServer\build\testresults\html\index.html.

Creating a Curam Server Test case

Before you create a Curam server test case, you need to ensure that curam.test.framework.CuramServerTest is in your build path.  To do this, just set EJBServer\components\core\tests as a source folder and set it to include only curam.test.framework.CuramServerTest.  The other files are used by the build scripts and won't compile cleanly in Eclipse without making additional changes to the build path that you don't really need.

I would the create a simple test case that looks like:

public CuramSanityTest extends CuramServerTest {
  /**
   * This is needed because Curam is using an
   * older version of JUnit which requires the
   * one argument constructor.
   */
  public CuramSanityTest(final String name) {
    super(name);
  }
 
  public void testSanity() throws Exception {
    assertTrue(true);
  }
 
  public void testOrganizationListUserForOrganization() throws Exception {
    OrganizationFactory.newInstance().listUserForOrganisation(); 
  }
}

Then I rerun the test to see if it works out on Eclipse and on the command line.

Special directories

Curam's command line tester will test every single file in the tests.jar with the exception of a few folders unless you modify app_test.xml.  However, if you want to avoid having to make changes to the build scripts, use the server.curam.test.utility package to contain your utility classes.

Setup and Teardown

Curam's test framework extends the setUp and tearDown methods with setUpCuramServerTest and tearDownCuramServerTest respectively. Unfortunately, they did not make them throw Exception.  So unless you make your own copy of the CuramServerTest, which I normally do in my projects to eliminate the need for the constructor, you would have to wrap your setUp and tearDown routines with a try-catch block that throws a RunTimeException for example:

protected void setUpCuramServerTest() {
  try {
    ...
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

Thanks

Kudos to Robert S. for encouraging me to write this.  Good luck on your project Rob and sorry if this is simpler than the way you tried to copy from us!

Sunday, September 23, 2007

Generating a random SIN

The Canadian Social Insurance Number is something that would validate against the Luhn algorithm. Since SINs may be used as input data for some applications, I have translated the code that generates a number that passes the Luhn Algorithm in Wikipedia to something that can be put into into Selenium's user-extensions.js.

Selenium.prototype.getRandomSIN = function() {
    var length = 9;
    var digits = new Array()
    for(var i = 0; i < length - 1; ++i) {
        digits[i] = Math.floor(Math.random() * 9)
    }
    var sum = 0;
    var alt = true;
    for(var i = length - 2; i >= 0; --i) {
        if (alt) {
            var temp = digits[i];
            temp *= 2;
            if (temp > 9) {
                temp -= 9;
            }
            sum += temp;
        } else {
            sum += digits[i];
        }
        alt = !alt;
    }
    var modulo = sum % 10;
    if(modulo > 0) {
        digits[length-1] = 10 - modulo;
    }
    return digits.join("");
};

Thursday, December 07, 2006

Storing the current date in Selenium scripts

In my application I needed to put in the current date and it has to be in a specific format (yyyy-MMM-d) so taking the information I gathered from openacs.org/xowiki/en/Testing_with_Selenium I put in the following near the beginning of my Selenium script. The cool part here being the way I created an anonymous array that stores the text. Note the text between {} should all be in one line but is broken apart for clarity (and to make it fit in the window).

storeExpression
javascript{
var d = new Date();
d.getFullYear() + "-" +
[ "Jan", "Feb", "Mar",
  "Apr", "May", "Jun",
  "Jul", "Aug", "Sep",
  "Oct", "Nov", "Dec"]
[d.getMonth()] +
"-" + d.getDate()
}
today
And then to type it in a field.
type
__o3fwp.ACTION.dtls$dtls$acStaffingDate
${today}