I thought I understood CodePro's contracts, but they seem to have no effect. For example:
public class ContractTest {
private int number;
/**
* @pre inputNumber > 0
*
* Alternatively:
* @post number > 0
*/
public void setNumber(int inputNumber) {
number = inputNumber;
}
public int getNumber() {
return number;
}
public static void main(String args[]) {
ConditionsTest conditionsTest = new ConditionsTest();
conditionsTest.setNumber(-5);
System.out.println("Number: " + conditionsTest.getNumber());
}
}
Running the main(String[]) method causes:
number: -5
to be printed. There were no compile warning (expected), and no exceptions thrown. Also, the junit test methods generated by CodePro were not affected by the contracts.
So how do you use CodePro's contracts?
Are you sure you are supposed to get compilation warnings? From what I've seen, contracts in CodePro are only meant to generate JUnit test cases with the proper asserts, not to give warnings.
If you want to include design by contracts in your java development, Cofoja is definitely a better choice:
http://code.google.com/p/cofoja/
Edit: Setting up Cofoja in Eclipse:
http://fsteeg.com/2011/02/07/setting-up-contracts-for-java-in-eclipse/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With