Over a million developers have joined DZone.

Java Reflection Library Duckapter 0.7.0 Beta

DZone's Guide to

Java Reflection Library Duckapter 0.7.0 Beta

Free Resource

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);
for (TestMethod method : tests.testMethods()) {
try {
} catch (Exception e) {

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


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}