Trait testing with JUnit.
Add the following dependency to your pom.xml file:
<dependency>
<groupId>com.jnape.palatable</groupId>
<artifactId>traitor</artifactId>
<version>1.0</version>
</dependency>`An example that asserts a trait against a subject:
class FunctionalIterableTest {
@Test
void laziness() {
assertTrait(Laziness.class, FunctionalIterable.iterable(1, 2, 3, 4));
}
}This example is implemented in the style of Junit 5, but since the only integration with traitor necessary is a static method call, this should be compatible with any Java test framework.
junit 4 integration
With Junit 4, traitor supports a Runner-based integration. To use it, modify the dependency on traitor:
<dependency>
<groupId>com.jnape.palatable</groupId>
<artifactId>traitor-junit4</artifactId>
<version>2.0.0</version>
</dependency>An example test suite that tests traits alongside unit tests might look like:
@RunWith(Traits.class)
public class FunctionalIterableTest {
@Test
public void mapsFunctionOverValues() {
//...
}
@TestTraits({Laziness.class, StandardIteration.class})
public FunctionalIterable createTestSubject() {
return FunctionalIterable.iterable(1, 2, 3, 4, 5);
}
}public class Laziness implements Trait<FunctionalIterable> {
@Override
public void test(FunctionalIterable testSubject) {
//use normal JUnit asserts here
}
//same goes for StandardIteration
}Note that the Traits runner simply requires the existence of at least @Test method or @TestTrait method in your test suite.
Traitor can be run the same way you run JUnit - typically in your IDE or using your build tool (Maven, Gradle, Ant, etc.). Any test suites that use the Traits runner will automatically work.
traitor is part of palatable, which is distributed under The MIT License.
