Tuesday, October 20, 2009

Default Values








Default Values


The reason for ignoring a test method is likely to be the same most of the time. Most often, you will want to temporarily comment out a test while fixing other broken tests. Supplying a reason each time can be onerous, so you would like to have a default ignore reason. Here's how you might reflect this need in a TestRunner test:




@TestMethod
public void ignoreWithDefaultReason() {
runTests(DefaultIgnoreMethodTest.class);
verifyTests(methodNameA, methodNameB);
Map<Method, Ignore> ignoredMethods = runner.getIgnoredMethods();
Map.Entry<Method, Ignore> entry = getSoleEntry(ignoredMethods);
Ignore ignore = entry.getValue();
assert TestRunner.DEFAULT_IGNORE_REASON.
equals(ignore.reasons()[0]);
}

class DefaultIgnoreMethodTest {
@TestMethod public void testA() {}
@TestMethod public void testB() {}
@Ignore(initials=TestRunnerTest.IGNORE_INITIALS)
@TestMethod public void testC() {}
}


You will need to define the constant DEFAULT_IGNORE_REASON in the TestRunner class to be whatever string you desire:



class TestRunner {
public static final String DEFAULT_IGNORE_REASON =
"temporarily commenting out";
// ...


You can supply a default value on any annotation type member. The default must be a constant at compile time. The new definition of @Ignore includes a default value on the reasons member. Note use of the keyword default to separate the member signature from the default value.



package sis.testing;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Ignore {
String[] reasons() default TestRunner.DEFAULT_IGNORE_REASON;
String initials();
}








    No comments: