In the following example MyManager uses interface MyInterface, but the implementor of MyManager has not honoured the contract @Pre("!=null"). (Fix for this could be e.g. using Google Guava's Strings.nullToEmpty in the callDo)
(Imports are not showed in the following snippets to save vertical space)
MyInterface.java :
public interface MyInterface {
@Post("result == 'success' || result == 'failure'")
String doIt(@Pre("!=null") String text);
}
MyManager.java :
public class MyManager {
private final MyInterface myInterface;
public MyManager(MyInterface myInterface) {
this.myInterface = myInterface;
}
public void callDo(String text) {
myInterface.doIt(text);
}
}
MyManagerTest.java :
public class MyManagerTest extends EasyMockSupportWithDbC {
@Test
public void test() {
MyInterface myInterface = createNiceMock(MyInterface.class);
MyManager myManager = new MyManager(myInterface);
replayAll();
myManager.callDo(null); // this will throw PreconditionException
verifyAll();
}
}
Also the implementor of MyInterface has been lazy, and has not honoured the postcondition @Post("result == 'success' || result == 'failure'").
MyObject.java :
public class MyObject implements MyInterface {
@Override
public String doIt(String text) {
if (text.equals("password")) {
return "success";
}
return "";
}
}
MyObjectTest.java :
public class MyObjectTest extends EasyMockSupportWithDbC {
@Test
public void test() {
MyObject myObject = dbc(new MyObject());
myObject.doIt("password");
myObject.doIt("teststring");
}
}