TestRunnerTest


The first test in TestRunnerTest, singleMethodTest, goes up against a secondary class SingleMethodTest defined in the same source file. SingleMethodTest provides a single empty test method that should result in a pass, as it would in JUnit.

So far you need no annotations. You pass a reference to the test class, TestRunnerTest.class, to an instance of TestRunner. TestRunner can assume that this parameter is a test class containing only test methods.

To generate test failures, you will use the assert facility in Java, described in the first part of this lesson. Remember that you must enable assertions when executing the Java VM; otherwise, Java will ignore them.

Here is TestRunnerTest.

 package sis.testing; import java.util.*; import java.lang.reflect.*; public class TestRunnerTest {    public void singleMethodTest() {       TestRunner runner = new TestRunner(SingleMethodTest.class);       Set<Method> testMethods = runner.getTestMethods();       assert 1 == testMethods.size() : "expected single test method";       Iterator<Method> it = testMethods.iterator();       Method method = it.next();       final String testMethodName = "testA";       assert testMethodName.equals(method.getName()) :          "expected " + testMethodName + " as test method";       runner.run();       assert 1 == runner.passed() : "expected 1 pass";       assert 0 == runner.failed() : "expected no failures";    }    public void multipleMethodTest() {       TestRunner runner = new TestRunner(MultipleMethodTest.class);       runner.run();       assert 2 == runner.passed() : "expected 2 pass";       assert 0 == runner.failed() : "expected no failures";       Set<Method> testMethods = runner.getTestMethods();       assert 2 == testMethods.size() : "expected single test method";       Set<String> methodNames = new HashSet<String>();       for (Method method: testMethods)          methodNames.add(method.getName());       final String testMethodNameA = "testA";       final String testMethodNameB = "testB";       assert methodNames.contains(testMethodNameA):          "expected " + testMethodNameA + " as test method";       assert methodNames.contains(testMethodNameB):          "expected " + testMethodNameB + " as test method";    } } class SingleMethodTest {    public void testA() {} } class MultipleMethodTest {    public void testA() {}    public void testB() {} } 

The second test, multipleMethodTest, is a bit of a mess. To create it, I duplicated the first test and modified some of the details. It screams out for refactoring. The problem is that as soon as you extract a common utility method in TestRunnerTest, the TestRunner class will assume it's a test method and attempt to execute it. The solution will be to introduce an annotation that you can use to mark and distinguish test methods.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net