如何在 Eclipse 中使用 CodePro 的合约?
我以为我理解了CodePro的合约,但它们似乎没有任何作用。例如:
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());
}
}
运行 main(String[]) 方法会导致
number: -5
打印:。没有编译警告(预期),也没有抛出异常。此外,CodePro 生成的 junit 测试方法不受合约的影响。
那么如何使用CodePro的合约呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定应该收到编译警告吗?据我所知,CodePro 中的合约仅用于生成具有正确断言的 JUnit 测试用例,而不是发出警告。
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.
如果您想在 Java 开发中包含契约设计,Cofoja 绝对是更好的选择:
http:// code.google.com/p/cofoja/
编辑:
在 Eclipse 中设置 Cofoja:
http:// fsteeg.com/2011/02/07/setting-up-contracts-for-java-in-eclipse/
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/