Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

How to write unit test case using J2ME unit ?

From Wiki
Jump to: navigation, search

Needs-update.pngThis 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.

Article Metadata
Article
Created: senthilkumar05 (26 Dec 2007)
Last edited: hamishwillee (25 Jul 2013)
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);
}
 
}
This page was last modified on 25 July 2013, at 04:12.
39 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×