<rdf:RDF
    xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
    xmlns:s='http://snipsnap.org/rdf/snip-schema#'
    xml:base='http://www.mobile-j.de/snipsnap/rdf'>
    <s:Snip rdf:about='http://www.mobile-j.de/snipsnap/rdf#J2ME/Unit+testing+with+JMUnit+and+NetBeans+IDE'
         s:cUser='bjoernQ'
         s:oUser='bjoernQ'
         s:mUser='bjoernQ'>
        <s:name>J2ME/Unit testing with JMUnit and NetBeans IDE</s:name>
        <s:content>{link:Unit testing|http://en.wikipedia.org/wiki/Unit_testing} is common practice in professional software development. Especially in the Java EE space it&apos;s widely used to ensure stable high quality software.&#xD;&#xA;&#xD;&#xA;While unit testing is a methodology and doesn&apos;t depend on a special framework it&apos;s a good idea to use a mature framework.&#xD;&#xA;&#xD;&#xA;One of the frameworks available in the Java ME space is {link:JMUnit|http://sourceforge.net/projects/jmunit/}.&#xD;&#xA;&#xD;&#xA;Here I will give you a quick overview of the steps involved to use JMUnit and NetBeans 6.1 to easily write &#xD;&#xA;&#xD;&#xA;and run Java ME unit tests and how to create a release build without any test artifacts contained.&#xD;&#xA;&#xD;&#xA;When you have created a Mobile Java Appication right click on your desired package node and choose &quot;New / Empty JUnit Test&quot;.&#xD;&#xA;&#xD;&#xA;Now create your Test-Class. It&apos;s a good idea to have a naming convention like &quot;*Test&quot; for test classes.&#xD;&#xA;&#xD;&#xA;NetBeans 6.1 automatically adds a dependency on JMUnit4CLDC10. If you intent to you use CLDC1.1 you should remove that dependency and use JMUnit4CLDC11. Don&apos;t forget to change the import statement of the generated class.&#xD;&#xA;&#xD;&#xA;Now you have a new test class consisting of a constructor and a method named &quot;test(int testNumber)&quot;.&#xD;&#xA;&#xD;&#xA;In the constructor there&apos;s a call to the super constructor. Here you have to give the total number of tests to run.&#xD;&#xA;&#xD;&#xA;In the test method you should dispatch the call to the actual test method.&#xD;&#xA;&#xD;&#xA;Example:&#xD;&#xA;{code}&#xD;&#xA;public class NewEmptyJMUnitTest extends TestCase {&#xD;&#xA;    &#xD;&#xA;    public NewEmptyJMUnitTest() {&#xD;&#xA;        //The first parameter of inherited constructor is the number of test cases&#xD;&#xA;        super(1,&quot;NewEmptyJMUnitTest&quot;);&#xD;&#xA;    }            &#xD;&#xA;&#xD;&#xA;    public void test(int testNumber) throws Throwable {&#xD;&#xA;        switch(testNumber){&#xD;&#xA;            case 0: &#xD;&#xA;                testOne();&#xD;&#xA;                break;&#xD;&#xA;        }&#xD;&#xA;    }&#xD;&#xA;&#xD;&#xA;    private void testOne() throws Exception{&#xD;&#xA;        // here we run our test, use assertXXX methods to check results&#xD;&#xA;        String tmp = &quot;Hello World&quot;;&#xD;&#xA;        assertEquals(tmp, &quot;Hello World!&quot;);&#xD;&#xA;    }&#xD;&#xA;}&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;Now it&apos;s time to run the test. Right click on the project node, choose &quot;Properties&quot;. Choode &quot;Application Descriptor&quot;.&#xD;&#xA;&#xD;&#xA;Activate &quot;MIDlets&quot; and click &quot;Add...&quot;.&#xD;&#xA;&#xD;&#xA;Choose the test newly created test class and give it a good name indicating that it&apos;s the testrunner.&#xD;&#xA;&#xD;&#xA;When you launch the application there are two MIDlets to choose from: Your original MIDlet and the Test-Runner-MIDlet.&#xD;&#xA;&#xD;&#xA;If there was any error it&apos;s details are dumped to the console. For the above test the output should look like this:&#xD;&#xA;&#xD;&#xA;{code}&#xD;&#xA;Assert Equals failed.&#xD;&#xA;Expected Hello World, but was Hello World!&#xD;&#xA;jmunit.framework.cldc11.AssertionFailedException&#xD;&#xA;        at jmunit.framework.cldc11.Assertion.fail(Assertion.java:1066)&#xD;&#xA;        at jmunit.framework.cldc11.Assertion.fail(Assertion.java:1054)&#xD;&#xA;        at jmunit.framework.cldc11.Assertion.assertEquals(Assertion.java:150)&#xD;&#xA;        at jmunit.framework.cldc11.Assertion.assertEquals(Assertion.java:594)&#xD;&#xA;        at hello.NewEmptyJMUnitTest.testOne(+9)&#xD;&#xA;        at hello.NewEmptyJMUnitTest.test(NewEmptyJMUnitTest.java:26)&#xD;&#xA;        at jmunit.framework.cldc11.TestCase.test(TestCase.java:65)&#xD;&#xA;        at jmunit.framework.cldc11.Screen.run(Screen.java:157)&#xD;&#xA;{code}&#xD;&#xA;&#xD;&#xA;Now it&apos;s time to correct the error and add tests to the test class. Don&apos;t forget to add the method call to the &quot;test(int)&quot; method and raise the number of tests in the call to the super constructor.&#xD;&#xA;&#xD;&#xA;When everything works fine and you are about to release your work you most likely don&apos;t want to ship the testing stuff with it.&#xD;&#xA;&#xD;&#xA;To do this you can create an additional project configuration. Right click on the project node and choose &quot;Properties&quot;.&#xD;&#xA;&#xD;&#xA;Choose &quot;Add configuration...&quot; from the project configuration combo box. Choose a good name for the configuration. (e.g. &quot;Release&quot;)&#xD;&#xA;&#xD;&#xA;In the release configuration choose MIDlets. Uncheck the &quot;Use values from &quot;DefaultConfiguration&quot;&quot; and remove the test runner MIDlet.&#xD;&#xA;&#xD;&#xA;Choose &quot;Build/Libraries &amp; Resources&quot; and remove the JMUnit dependency.&#xD;&#xA;&#xD;&#xA;Choose &quot;Build/Sources Filtering&quot; and check the &quot;Exclude Test Souces&quot;. If there are still test resources left, uncheck them too.&#xD;&#xA;&#xD;&#xA;You might wish to change other settings (e.g. obfuscation level) for the release, too.&#xD;&#xA;&#xD;&#xA;Now you can build the release e.g. via NetBeans&apos; batch build. You will find the release binaries in &quot;.../dist/Release&quot;.&#xD;&#xA;</s:content>
        <s:mTime>2008-12-30 11:37:38.797</s:mTime>
        <s:cTime>2008-12-30 11:32:20.897</s:cTime>
        <s:comments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
        <s:snipLinks
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
        <s:attachments
             rdf:type='http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag'/>
    </s:Snip>
</rdf:RDF>
