Array Parameters


You want to allow developers to provide multiple separate reason strings. To do so, you can specify String[] as the return type for the annotation type member value. An @Ignore annotation can then contain multiple reasons by using a construct that looks similar to an array initializer:


 @Ignore({"why", "just because"}) 

Here's the updated annotation type declaration:

 package sis.testing; import java.lang.annotation.*; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Ignore {    String[] value(); } 

If you only need to supply a single string for an annotation type member with a return type of String[], Java allows you to eliminate the array-style initialization. These annotations for the current definition of @Ignore are equivalent:

 @Ignore("why") @Ignore({"why"}) 

You will need to modify TestRunnerTest to support the change to Ignore.

 package sis.testing; import java.util.*; import java.lang.reflect.*; public class TestRunnerTest {    public static final String IGNORE_REASON1 = "because";    public static final String IGNORE_REASON2 = "why not";    ...    @TestMethod    public void ignoreMethodTest() {       runTests(IgnoreMethodTest.class);       verifyTests(methodNameA, methodNameB);       assertIgnoreReasons();    }    private void assertIgnoreReasons() {       Map<Method, Ignore> ignoredMethods = runner.getIgnoredMethods();       Map.Entry<Method, Ignore> entry = getSoleEntry(ignoredMethods);       assert "testC".equals(entry.getKey().getName()):          "unexpected ignore method: " + entry.getKey();       Ignore ignore = entry.getValue();       String[] ignoreReasons = ignore.value();       assert 2 == ignoreReasons.length;       assert IGNORE_REASON1.equals(ignoreReasons[0]);       assert IGNORE_REASON2.equals(ignoreReasons[1]);    }    ... } class SingleMethodTest {    @TestMethod public void testA() {} } class MultipleMethodTest {    @TestMethod public void testA() {}    @TestMethod public void testB() {} } class IgnoreMethodTest {    @TestMethod public void testA() {}    @TestMethod public void testB() {}    @Ignore({TestRunnerTest.IGNORE_REASON1,             TestRunnerTest.IGNORE_REASON2})       @TestMethod public void testC() {} } 



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