TestedBy: examples
Submitted by maeste on Wed, 10/15/2008 - 14:16
Simple example:
public class TestedBySample {
/**
* @param args
*/
public static void main( String[] args ) {
TestedBySample sample = new TestedBySample();
System.out.print(sample.add(1, 2));
}
@TestedBy( testClass = "it.javalinux.testedby.TestedBySampleTest", testMethod = "addShouldWork" )
public int add( int i,
int j ) {
return i + j;
}
@TestedByList( {@TestedBy( testClass = "it.javalinux.testedby.TestedBySampleTest", testMethod = "addShouldWork" ),
@TestedBy( testClass = "it.javalinux.testedby.TestedBySampleTest", testMethod = "addShouldWork2" )} )
public int add2( int i,
int j ) {
return i + j;
}
}
Here is an example on how TestedBy run test defined for an interface on all implementer: we have added @TestedBy annotation on APIInterface and then provided a @BeforeTestedBy annotation to set the interface instance on which run tests. Then testedBy will run shouldAddTwoAndThree both for APIImplOne and APIImplTwo, succeding on the first one and failing on the second
public interface APIInterface {
@TestedBy( testClass = "it.javalinux.testedby.APITest", testMethod = "shouldAddTwoAndThree" )
public int add(int a, int b);
}
public class APIImplOne {
public int add(int a, int b) {
return a + b;
}
public class APIImplTwo {
public int add(int a, int b) {
return a - b;
}
public class APITest {
private APIInterface instance;
@BeforeTestedBy
public beforeTestedBy(APIInterface instance) {
this.instance = instance;
}
public void shouldAddTwoAndThree() {
assertThat(instance.add(3,2), is(5));
}
}