@RunWith(RobolectricTestRunner.class)
public class ContractExampleTest {
@Test
public void testPreconditions() {
assertThrows(PreconditionError.class, () -> {
// Given...
Dictionary<Integer> dict = new Dictionary<>(1);
dict.put("foo", 1);
// When...
dict.put("bar", 2);
// Then... PreconditionError, not enough capacity!
});
}
@Invariant({
"capacity() >= count()",
"count() >= 0"
})
static
class Dictionary<TValue> {
private int _capacity;
private Map<String, TValue> _contents;
public int capacity() {
return _capacity;
}
public int count() {
return _contents.size();
}
@Requires("capacity > 0")
@Ensures({
"capacity() > 0",
"count() == 0"
})
public Dictionary(int capacity) {
this._capacity = capacity;
this._contents = new HashMap<>(capacity);
}
@Requires({
"key != null",
"key.length() > 0",
"count() < capacity()"
})
@Ensures({
"get(key) == value",
"count() == old(count()) + 1"
})
public void put(String key, TValue value) {
_contents.put(key, value);
}
}
}
Hi, in testPreconditions() I inserts twice, so the "capacity() >= count()" in preConditionTest should be violated Because capacity() returns 1, count() returns 2.
But when I executed the test, I got "expected com.google.java.contract.PreconditionError to be thrown, but nothing was thrown."
I have no idea why nothing was thrown. Error message is "expected com.google.java.contract.PreconditionError to be thrown, but nothing is thrown"
Do anyone have some ideas?
Thank you!
Aucun commentaire:
Enregistrer un commentaire