August 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

A Unit Testing Framework for the BlackBerry

  • July 24, 2008
  • By Jeff Langr
  • Send Email »
  • More Articles »

Listing 2: AssertTest.

package com.langrsoft.bbtest;

import junit.framework.*;

public class AssertTest extends TestCase {
   public void testAssertTruePasses() {
      com.langrsoft.bbtest.Assert.assertTrue(true);
   }

   public void testAssertFalseFails() {
      try {
         com.langrsoft.bbtest.Assert.assertTrue(false);
         junitFail();
      } catch (com.langrsoft.bbtest.AssertionException
         expected) {
         junit.framework.Assert.assertEquals("",
            expected.getMessage());
      }
   }

   public void testAssertEqualsPassesWithTwoEqualValues() {
      com.langrsoft.bbtest.Assert.assertEquals(0, 0);
   }

   public void testAssertEqualsFailsWithTwoDifferentValues() {
      try {
         com.langrsoft.bbtest.Assert.assertEquals(0, 1);
         junitFail();
      } catch (com.langrsoft.bbtest.AssertionException
         expected) {
         junit.framework.Assert.assertEquals("expected <0>
            but was <1>", expected.getMessage());
      }
   }

   private void junitFail() {
      junit.framework.Assert.fail();
   }

   public void testAssertEqualsPassesWithTwoEqualReferences() {
      com.langrsoft.bbtest.Assert.assertEquals(new Integer(15),
         new Integer(15));
   }

   public void testAssertEqualsFailsWithTwoUnequalReferences() {
      try {
         com.langrsoft.bbtest.Assert.assertEquals(new Integer(15),
            new Integer(16));
         junitFail();
      } catch (com.langrsoft.bbtest.AssertionException expected) {
         junit.framework.Assert.assertEquals("expected <15>
            but was <16>", expected.getMessage());
      }
   }

   public void testAssertEqualsPassesWithTwoNullReferences() {
      com.langrsoft.bbtest.Assert.assertEquals(null, null);
   }

   public
      void testAssertEqualsFailsWithNullExpectedNonNullActual() {
      try {
         com.langrsoft.bbtest.Assert.assertEquals(null,
            new Integer(16));
         junitFail();
      } catch (com.langrsoft.bbtest.AssertionException
         expected) {
         junit.framework.Assert.assertEquals("expected <null>
            but was <16>", expected.getMessage());
      }
   }

   public void testFailFails() {
      try {
         com.langrsoft.bbtest.Assert.fail();
         junitFail();
      } catch (com.langrsoft.bbtest.AssertionException
         expected) {
         junit.framework.Assert.assertEquals("",
            expected.getMessage());
      }
   }
}

AssertTest is not at all complete. It provides only two overloaded versions of assertEquals—one for object references, one for int. I'll add the remainder as necessity calls. The assert methods in AssertTest do not provide "message strings"—strings to display when the test fails, mostly because I almost never use them (I can usually find a better way to make my tests more meaningful). I'll add them in the near future, if only to be more consistent with other frameworks.

Assertion failures are simple runtime exceptions of the type AssertionFailure, as Listing 3 shows.

Listing 3: Assert.

package com.langrsoft.bbtest;

public class Assert {
   public static void assertTrue(boolean actual) {
      if (!actual)
         throw new AssertionException("");
   }

   public static void assertEquals(int expected, int actual) {
      if (expected != actual)
         throwUnequal(String.valueOf(expected),
            String.valueOf(actual));
   }

   private static void throwUnequal(Object expected,
      Object actual) {
      throw new AssertionException("expected <" + expected +
         "> but was <" + actual + ">");
   }

   public static void assertEquals(Object expected, Object
      actual) {
      if ((expected == null && actual != null) ||
          (expected != null && !expected.equals(actual)))
         throwUnequal(expected, actual);
   }

   public static void fail() {
      throw new AssertionException("");
   }
}

Next, the MicroTest class, which represents a singular test to be executed. Listing 4 contains both MicroTest, its base class (which is also used by MicroTestGroup, presented later), and the interfaces it uses.





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel