Problems with the JMUnit TestSuite Class


I attempted to use the ‘JMUnit’ unit testing library that is shipped with NetBeans for the first time today. I created a couple of simple TestCase classes and a TestSuite that combines each of these. However, every time I attempted to load the test suite I was presented with the following exception:

result 0: first.AdderTest
java.lang.SecurityException: MIDlet not constructed by createMIDlet.
        at com.sun.midp.midlet.MIDletStateHandler.newMIDletPeer(), bci=24
        at javax.microedition.midlet.MIDlet.(), bci=9
        at jmunit.framework.cldc10.Assertion.(), bci=1
        at jmunit.framework.cldc10.Test.(), bci=1
        at jmunit.framework.cldc10.TestCase.(), bci=2
        at first.AdderTest.(), bci=4
        at java.lang.Class.newInstance(), bci=0
        at jmunit.framework.cldc10.TestSuite.(), bci=57
        at java.lang.Class.newInstance(), bci=0
        at com.sun.midp.main.CldcMIDletLoader.newInstance(), bci=46
        at com.sun.midp.midlet.MIDletStateHandler.createMIDlet(), bci=66
        at com.sun.midp.midlet.MIDletStateHandler.createAndRegisterMIDlet(), bci=17
        at com.sun.midp.midlet.MIDletStateHandler.startSuite(), bci=27
        at com.sun.midp.main.AbstractMIDletSuiteLoader.startSuite(), bci=52
        at com.sun.midp.main.CldcMIDletSuiteLoader.startSuite(), bci=8
        at com.sun.midp.main.AbstractMIDletSuiteLoader.runMIDletSuite(), bci=161
        at com.sun.midp.main.AppIsolateMIDletSuiteLoader.main(), bci=26

The above exception indicates (emphasized in bold) that the problem originated from the constructor of my test case class.

// AdderTest.java

import jmunit.framework.cldc10.*;

public class AdderTest extends TestCase {
   public AdderTest() {
      // This is where the problem occurs!
      super(1, "AdderTest");
   }
   public void test(int testNumber) throws Throwable {
      // Note: 'testNumber' doesn't matter as there is only one test!
      int result = new Adder().add(1, 2);
      System.out.println("1 + 2 = " + result);
   }
}

// Adder.java

public class Adder {
   public int add(int a, int b) {
      return a + b;
   }
}

As you can see from the above source the constructor in question was extremely simple. The exception was being generated as a result of instantiating the AdderTest class. Surprisingly no such exceptions were being shown when test suites were not being used.

After a little digging I discovered that this problem is in fact due to a security constraint that is imposed by the J2ME specification. A MIDlet cannot attempt to create another MIDlet from within its constructor (See: http://sites.google.com/site/franciscobenitezleon/projects/jmunit-revised).

Thankfully Francisco Benítez León has come up with a solution to this problem by providing several customizations that allow us to workaround this problem. The various driver classes are no longer derived from a MIDlet, and thus avoid the security exception. This does, however, mean that we must create a simple MIDlet that kick-starts the testing process.

Refer to the following link for a download of this library patch and for further information:
http://sites.google.com/site/franciscobenitezleon/projects/jmunit-revised/jmunit-revised-2-0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s