Vladimír has posted 1 posts at DZone. View Full User Profile

Java Reflection Library Duckapter 0.7.0 Beta

04.27.2010
| 2675 views |
  • submit to reddit

Java reflection and duck typing library Duckapter 0.7.0 beta release is now available

The Duckapter library provides interface based duck typing support for the Java programming language but can be also use to simplify usage of the Java Reflection API. The duck tests are created using interfaces and "checker" annotations which for example exist for each Java modifier. If the test succeed the underlying object's or class' members can be accessed using the same interface as used for the test. The Duckapter was originally designed to allow writing white-box in-depth test inside unit tests but can be also used for example bycreators of annotation based or convention-over-configuration based libraries and frameworks

Following complex example shows how could be JUnit 4 test case described using the Duckapter library:


public interface JUnit4Test {

@DoesNotHave @NonStatic @BeforeClass
void beforeClassMustBeStatic();

@DoesNotHave @Static @Before
void setUpCannotBeStatic ();

@DoesNotHave @Static @Test
void testsCannotBeStatic();

@DoesNotHave @Static @After
void tearDownCannotBeStatic();

@DoesNotHave @NonStatic @AfterClass
void afterClassMustBeStatic();



@DoesNotHave @Package(AT_MOST) @BeforeClass
void beforeClassMustBePublic();

@DoesNotHave @Package(AT_MOST) @Before
void setUpMustBePublic();

@DoesNotHave @Package(AT_MOST) @Test
void testsMustBePublic();

@DoesNotHave @Package(AT_MOST) @After
void tearDownMustBePublic();

@DoesNotHave @Package(AT_MOST) @AfterClass
void afterClassMustBePublic();


@Optional @Static @BeforeClass void beforeClass()throws Exception;
@Optional @Before void setUp()throws Exception;
@Optional @After void tearDown()throws Exception;
@Optional @Static @AfterClass void afterClass()throws Exception;

@All @Test TestMethod[] testMethods();

The TestMethod interface has only one method public void test() throws Exception. You can also create a simple test runner using the interface above.


JUnit4Test tests = Duck.type(testing, JUnit4Test.class);
tests.beforeClass();
for (TestMethod method : tests.testMethods()) {
try {
tests.setUp();
method.test();
tests.tearDown();
} catch (Exception e) {
handleException(e);
}
}
tests.afterClass();

More information and examples you can find on the project's web site: http://code.google.com/p/duckapter

0
Published at DZone with permission of its author, Vladimír Oraný.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)