|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--j2meunit.framework.Assert | +--j2meunit.framework.TestCase
A test case defines the fixture to run multiple tests. To define a test case
setUp
tearDown
.
public class MathTest extends TestCase { protected int nValue1; protected int nValue2; protected void setUp() { nValue1= 2; nValue2= 3; } }For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling
assertTrue
(or another assert method) with a boolean:
public void testAdd() { double result= nValue1 + nValue2; assertTrue(result == 5); }Once the methods are defined you can run them. The framework supports both a static type safe and more dynamic way to run a test. In the static way you override the runTest method and define the method to be invoked. A convenient way to do so is with an anonymous inner class:
TestCase test= new MathTest("add") { public void runTest() { testAdd(); } }; test.run();In JUnit, the dynamic way uses reflection in the default implementation of
runTest
. Because reflection is not available in J2ME, here a
different approach is necessary. Together with a method name, an instance
of the class TestMethod can be given to the constructor of TestCase. It can
be created as an anonymous inner class that invokes the actual test method
in the implementation of the
runmethod:
TestCase test = new MathTest("testAdd", new TestMethod() { public void run(TestCase tc) { ((MathTest) tc).testAdd(); } }); test.run();To make this work subclasses need to implement the additional constructor and forward the parameters to the corresponding TestCase constructor. Multiple tests to be run can be grouped into a TestSuite. J2MEUnit provides different test runners which can run a test suite and collect the results. Because J2ME doesn't provide reflection, a test suite must always be created manually from test instances associated with TestMethods. The missing reflection is also the reason that the
suitemethod cannot be static in J2MEUnit:
public Test suite() { TestSuite suite = new TestSuite(); { suite.addTest(new MathTest("testAdd", new TestMethod() { public void run(TestCase tc) {((MathTest) tc).testAdd(); } })); suite.addTest(new MathTest("testAdd", new TestMethod() { public void run(TestCase tc) {((MathTest) tc).testDivideByZero(); } })); return suite; }
TestResult
,
TestSuite
,
TestMethod
Constructor Summary | |
TestCase()
Default constructor. |
|
TestCase(java.lang.String name)
Constructor for a named Test. |
|
TestCase(java.lang.String name,
TestMethod testMethod)
Constructor for a test that will execute a particular test method. |
Method Summary | |
int |
countTestCases()
Always return 1. |
int |
countTestSteps()
Returns the same as countTestCases. |
protected TestResult |
createResult()
Creates a default TestResult object |
java.lang.String |
getName()
Returns the test name. |
TestMethod |
getTestMethod()
Returns the Method to be executed by the test case instance. |
java.lang.String |
getTestMethodName()
Deprecated. Replaced by getName() |
protected void |
onAssertion()
Callback from the Assert base class that will be invoked on assertions. |
TestResult |
run()
A convenience method to run this test, collecting the results with a default TestResult object. |
void |
run(TestResult result)
Runs the test case and collects the results in TestResult. |
void |
runBare()
Runs the bare test sequence. |
protected void |
runTest()
The default implementation will run the TestMethod associated with the TestCase instance and asserts that it is not null. |
void |
setName(java.lang.String name)
To set the test name. |
void |
setTestMethod(java.lang.String methodName,
TestMethod testMethod)
Convenience method to set the name and wrapper of the method to be executed by the test case instance. |
void |
setTestMethod(TestMethod testMethod)
To set the method to be executed by the test case instance. |
void |
setTestMethodName(java.lang.String name)
Deprecated. Replaced by setName(String) |
protected void |
setUp()
Sets up the fixture, for example, open a network connection. |
Test |
suite()
This method should be overridden if a test case contains multiple test methods. |
protected void |
tearDown()
Tears down the fixture, for example, close a network connection. |
protected void |
testStepFinished()
Notifies listeners that a test step has finished. |
java.lang.String |
toString()
Returns a string representation of the test case. |
Methods inherited from class j2meunit.framework.Assert |
assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public TestCase()
public TestCase(java.lang.String name)
name
- The name of the test (method)public TestCase(java.lang.String name, TestMethod testMethod)
name
- The name of the test methodtestMethod
- The TestMethod wrapper for the method to executeMethod Detail |
public void setName(java.lang.String name)
name
- The new test namepublic java.lang.String getName()
public void setTestMethod(java.lang.String methodName, TestMethod testMethod)
methodName
- The name of the test methoto executetestMethod
- The method wrapperpublic void setTestMethod(TestMethod testMethod)
testMethod
- The TestMethod to executepublic TestMethod getTestMethod()
public void setTestMethodName(java.lang.String name)
name
- The test instance namepublic java.lang.String getTestMethodName()
public int countTestCases()
countTestCases
in interface Test
public int countTestSteps()
countTestSteps
in interface Test
Test.countTestSteps()
public TestResult run()
TestResult
public void run(TestResult result)
run
in interface Test
result
- The TestResult to collect the data inpublic void runBare() throws java.lang.Throwable
java.lang.Throwable
- if any exception is thrownpublic Test suite()
new TestSuite(new MyTestCase("testMethodOne", new TestMethod() { public void run(TestCase tc) { ((MyTestCase) tc).testMethodOne(); } }));
public java.lang.String toString()
toString
in class java.lang.Object
protected void setUp() throws java.lang.Exception
java.lang.Exception
- An arbitrary exception may be thrownprotected TestResult createResult()
TestResult
protected void runTest() throws java.lang.Throwable
java.lang.Throwable
- if any exception is thrownprotected void tearDown() throws java.lang.Exception
java.lang.Exception
- An arbitrary exception may be thrownprotected void onAssertion()
onAssertion
in class Assert
Assert.onAssertion()
protected void testStepFinished()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |