How to write unit test case using J2ME unit ?
This article needs to be updated: If you found this article useful, please fix the problems below then delete the {{ArticleNeedsUpdate}} template from the article to remove this warning.
Reasons: hamishwillee (27 Sep 2012)
This is a code dump, not an article. Ideally this should have some explanation added along with a downloadable zip file example, and a link to reference material on J2ME unit.
Reasons: hamishwillee (27 Sep 2012)
This is a code dump, not an article. Ideally this should have some explanation added along with a downloadable zip file example, and a link to reference material on J2ME unit.
Article Metadata
import j2meunit.midletui.TestRunner;
/*************************************************************************
* Example MIDlet TestRunner that overloads the startApp() method and invokes
* start() with the names of the classes to test. This can be achieved also by
* setting the JAD file attribute J2MEUnitTestClasses with the names of the
* classes to test and then invoke the superclass j2meunit.midletui.TestRunner
* directly without subclassing it.
*
*/
public class ExampleTestMidlet extends TestRunner
{
//~ Constructors -------------------------------------------------------
public ExampleTestMidlet()
{
}
/************************************
* startApp.
*/
protected void startApp()
{
start(new String[] { "j2meunit.examples.TestAll" });
}
}
import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestSuite;
(2)
public class TestAll extends TestCase
{
public TestAll()
{
super("null");
}
public TestAll(String name)
{
super(name);
}
//~ Methods ----------------------------------------------------------------
public static void main(String[] args)
{
String[] runnerArgs = new String[] { "j2meunit.examples.TestAll" };
j2meunit.textui.TestRunner.main(runnerArgs);
}
public Test suite()
{
TestSuite suite = new TestSuite();
suite.addTest(new TestOne().suite());
suite.addTest(new TestTwo().suite());
return suite;
}
}
(3)
import j2meunit.framework.*;
public class TestOne extends TestCase
{
public TestOne()
{
}
/***************************************
* TestOne constructor comment.
*
* @param name java.lang.String
*/
public TestOne(String sTestName, TestMethod rTestMethod)
{
super(sTestName, rTestMethod);
}
//~ Methods ------------------------------------------------------
/***************************************
* To create the test suite.
*/
public Test suite()
{
TestSuite aSuite = new TestSuite();
aSuite.addTest(new TestOne("testOne", new TestMethod()
{ public void run(TestCase tc) {((TestOne) tc).testOne(); } }));
aSuite.addTest(new TestOne("testTwo", new TestMethod()
{ public void run(TestCase tc) {((TestOne) tc).testTwo(); } }));
return aSuite;
}
/***************************************
* Test method 1.
*/
public void testOne()
{
System.out.println("TestOne.testOne()");
assertTrue("Should be true", false);
}
/***************************************
* Test method 2.
*/
public void testTwo()
{
System.out.println("TestOne.testTwo()");
throw new RuntimeException("Exception");
}
} // Endof TestOne class
(4)
import j2meunit.framework.*;
public class TestTwo extends TestCase
{
/**************************
TestTwo
constructor
*/
public TestTwo()
{
super("null");
}
public TestTwo(String sName, TestMethod rTestMethod)
{
super(sName, rTestMethod);
}
//~ Methods ----------------------------------------------------------------
/***************************************
* Create the test suite.
*/
public Test suite()
{
return new TestSuite(new TestTwo("testOne", new TestMethod()
{ public void run(TestCase tc)
{ ((TestTwo) tc).testOne(); }
}));
}
public void testOne()
{
System.out.println("TestTwo.testOne()");
assertEquals("These #'s should equal", 3, 4);
}
}


(no comments yet)