December 26, 2014
Hot Topics:

A Unit Testing Framework for the BlackBerry

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

Listing 5: MicroTestGroup.

package com.langrsoft.bbtest;

import java.util.*;

public class MicroTestGroup extends MicroTestBase implements
   Executable, TestContext {
   private Vector tests = new Vector();

   public void add(Executable executable) {
      tests.addElement(executable);
   }

   public void execute() {
      for (Enumeration e = tests.elements();
         e.hasMoreElements();) {
         Executable executable = (MicroTestBase)e.nextElement();
         executable.setListener(listener);
         executable.execute(this);
      }
   }

   public void execute(TestContext context) {
      execute();
   }

   public void tearDown() {
   }

   public void setUp() {
   }

   public void completed(MicroTest test) {
      listener.ran(test);
   }}

The method execute with a TestContext argument allows the MicroTest to call back to the containing MicroTestGroup's setUp and tearDown methods.

The final listing that I'll present (Listing 6) provides a simple result listener implementation, which demonstrates how a client user interface can use the information coming back from a test run. I use this listener for the test for MicroTestGroup.

Listing 6: A Simple Result Listener Implementation

package com.langrsoft.bbtest;

import java.util.*;

public class SimpleTestCollector implements ResultListener {
   Vector allTests     = new Vector();
   Vector passingTests = new Vector();
   Vector failingTests = new Vector();

   public void ran(MicroTest test) {
      if (test.passed)
         passingTests.addElement(test);
      else
         failingTests.addElement(test);
      allTests.addElement(test);
   }

   public int passed() {
      return passingTests.size();
   }

   public int failed() {
      return failingTests.size();
   }

   public void assertPassFailCounts(int expectedPassed,
      int expectedFailed) {
      junit.framework.Assert.assertEquals(expectedPassed,
         passed());
      junit.framework.Assert.assertEquals(expectedFailed,
         failed());
   }
}

So far, learning how to program for the BlackBerry has been an interesting challenge, mostly due to the constrained Java environment. I've started building a user interface that I can deploy to the BlackBerry, so that I can actually use the BBTest framework.

To me, the development of this framework is another testament to the value of test-driven development (TDD). I have to admit that the first cut wasn't as clean as what you see here. But, because I had the high level of test coverage that TDD provides, I was able to incrementally refactor to a much more elegant solution.

Download the Code

You can download the source code here.

About the Author

Jeff Langr is a veteran software developer with over a quarter century of professional software development experience. He's written two books, including Agile Java: Crafting Code With Test-Driven Development (Prentice Hall) in 2005. Jeff has contributed a couple chapters to Uncle Bob Martin's upcoming book, Clean Code. Jeff has written over 75 articles on software development, with over thirty appearing at Developer.com. You can find out more about Jeff at his site, http://langrsoft.com, or you can contact him via email at jeff at langrsoft dot com.





Page 4 of 4



Comment and Contribute

 


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

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel