Nested Test Case Classes

NestedTestCaseClasses is technique for OrganizingTestCases that consists of embedding unit tests right in the class they are testing.

This pattern has a number of advantages, some perceived disadvantages (easily dealt with), and some real issues that need watching, all discussed below.

The basic template for a class using this technique is:

 package wiki.example;
 class Demo {

// class body goes here

public static void main(String[] args) { junit.textui.TestRunner.run(TestCase.class); }

public static class TestCase extends junit.framework.TestCase { public void testDoSomething() { Demo d = new Demo(); d.doSomething(); assertEquals(control, d.field); } } }

Advantages Perceived Disadvantages
        NOTE: #if UNIT_TESTS is an easy way to wrap the test code so it 'disappears' in Ship builds.

Real Issues PaulKrause


EditText of this page (last edited October 31, 2009) or FindPage with title or text search