|
Example
j2meunit-generator example
Example of how to use j2me-unit-testingThis example of j2me unit testing is also part of the project source code. You can compile it and see how it works from there. You just have to set WTK_HOME environment variable so it points to your installation of sun wireless toolkit. In my case it is simply: set WTK_HOME=c:\wtk2.5.2 Please contact me if you have any problems. Example test casesLet's assume you have following two tests cases: /**
* Example test case containing two always-succeeding tests.
*
* @author Wojciech Mlynarczyk (woj)
*
*/
public class SuccessTest extends j2meunit.framework.TestCase {
public void testSuccess1() {
assertTrue(true);
}
public void testSuccess2() {
assertEquals(4, 2 + 2);
}
}and /**
* Example test case containing always-failing test.
*
* @author Wojciech Mlynarczyk (woj)
*
*/
public class FailureTest extends j2meunit.framework.TestCase {
public void testFailure() {
assertTrue(false);
}
}In order to execute unit tests on the device the project should also contain a test runner midlet, for example: /**
* Example test runner executing all the tests using generated test suite.
*
* @author Wojciech Mlynarczyk (woj)
*
*/
public class MyTestRunner extends j2meunit.midletui.TestRunner {
protected void startApp() {
// com.googlecode.wmlynar.j2meunitgenerator.example.AllTests is a generated class
start(new String[] { com.googlecode.wmlynar.j2meunitgenerator.example.AllTests.class.getName() });
}
/**
* Helpful for executing tests from command line / microemulator
*/
public static void main(String[] args) {
// com.googlecode.wmlynar.j2meunitgenerator.example.AllTests is a generated class
j2meunit.textui.TestRunner.main(new String[] { com.googlecode.wmlynar.j2meunitgenerator.example.AllTests.class.getName() });
}
}Generated test suiteJ2meunit generator will generate the following tests suite, that together with the test runner will execute all the tests: /**
* This class is generated by j2meunit-generator, DO NOT MODIFY
*
*/
public class AllTests extends TestCase {
public Test suite() {
TestSuite allSuites = new TestSuite("com.googlecode.wmlynar.j2meunitgenerator.example.AllTests");
TestSuite suite;
// com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest
suite = new TestSuite("com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest");
com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest test0;
test0 = new com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest() {
public void runTest() throws Throwable {
testSuccess1();
}
public String toString() {
return "testSuccess1(com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest)";
}
};
test0.setName("testSuccess1");
suite.addTest(test0);
test0 = new com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest() {
public void runTest() throws Throwable {
testSuccess2();
}
public String toString() {
return "testSuccess2(com.googlecode.wmlynar.j2meunitgenerator.example.tests.SuccessTest)";
}
};
test0.setName("testSuccess2");
suite.addTest(test0);
allSuites.addTest(suite);
// com.googlecode.wmlynar.j2meunitgenerator.example.tests.FailureTest
suite = new TestSuite("com.googlecode.wmlynar.j2meunitgenerator.example.tests.FailureTest");
com.googlecode.wmlynar.j2meunitgenerator.example.tests.FailureTest test1;
test1 = new com.googlecode.wmlynar.j2meunitgenerator.example.tests.FailureTest() {
public void runTest() throws Throwable {
testFailure();
}
public String toString() {
return "testFailure(com.googlecode.wmlynar.j2meunitgenerator.example.tests.FailureTest)";
}
};
test1.setName("testFailure");
suite.addTest(test1);
allSuites.addTest(suite);
return allSuites;
}
}When you look closer the code is really simple. It just generates one test for each method in the test case. It then collects those tests into a test suite and finally collects all test suites into one super test suite. |
Sign in to add a comment