我正在做一项下周到期的作业 所以基本上我有一个具有以下属性的 User 类
private String userName, password;
private final Instant registrationDate;
private HashSet<User> setSubs;
private ArrayList<Post> listPosts;
private int Index;
我尝试测试的方法是
public Post addPost(String msg) {
listPosts.add(new Post(msg));
Index++;
return new Post(msg);
}
和
public Post getPost(int i) {
return listPosts.get(i);
}
这是类 Post 属性
private final String textPost;
private final Instant dateCreation; //this.dateCreation = Instant.now();
private HashSet<User> likedUsers;
这是我面临的问题,在测试方法 addPost 时,它给出错误 org.opentest4j.AssertionFailedError: Expected: but was: ,当查看测试的类时,我注意到它在这一点上失败了
for (int i = 0; i < (self.getPostNb() - 1); i++) { //getPostNb() = listPost.size()
assertTrue(self.getPost(i).isAfter(self.getPost(i + 1)));
}
对于 getPost 方法,它会给出错误 java.lang.IllegalArgumentException:即使运行测试,绑定也必须为正
请问有什么建议可以修复它们吗? 谢谢
我需要 Junit 测试全部返回成功,但只有 addPost 和 getPost 失败,这使得使用它们的其他两个方法也被中止
编辑: 断言不变方法
public void assertInvariant(User self) {
// Put here the code to check the invariant:
// @invariant getName() != null && !getName().isBlank();
assertNotNull(self.getName());
assertFalse(self.getName().isBlank());
// @invariant getPassword() != null && !getPassword().isBlank();
assertNotNull(self.getPassword());
assertFalse(self.getPassword().isBlank());
// @invariant getRegistrationDate() != null;
assertNotNull(self.getRegistrationDate());
// @invariant getSubscriptions() != null && !getSubscriptions().contains(null);
assertNotNull(self.getSubscriptions());
assertFalse(self.getSubscriptions().contains(null));
// @invariant getSubscriptionNb() == getSubscriptions().size();
assertEquals(self.getSubscriptionNb(), self.getSubscriptions().size());
// @invariant getPosts() != null && !getPosts().contains(null);
assertNotNull(self.getPosts());
assertFalse(self.getPosts().contains(null));
// @invariant (\forall int i, j; i >= 0 && i < j && j < getPostNb(); getPost(i).isAfter(getPost(j)));
for (int i = 0; i < (self.getPostNb() - 1); i++) {
assertTrue(self.getPost(i).isAfter(self.getPost(i + 1)));
}
// @invariant getPostNb() == getPosts().size();
assertTrue(self.getPostNb() == self.getPosts().size());
// @invariant previousIndex() >= -1 && previousIndex() < getPosts().size();
assertTrue(self.previousIndex() >= -1);
assertTrue(self.previousIndex() < self.getPosts().size());
// @invariant nextIndex() >= 0 && nextIndex() <= getPosts().size();
assertTrue(self.nextIndex() >= 0);
assertTrue(self.nextIndex() <= self.getPosts().size());
// @invariant nextIndex() == previousIndex() + 1;
assertTrue(self.nextIndex() == self.previousIndex() + 1);
// @invariant lastIndex() >= -1 && lastIndex() < getPosts().size();
assertTrue(self.lastIndex() >= -1);
assertTrue(self.lastIndex() < self.getPosts().size());
// @invariant lastIndex() == previousIndex() || lastIndex() == nextIndex();
assertTrue(self.lastIndex() == self.previousIndex() || self.lastIndex() == self.nextIndex());
}
addPost的测试方法
@ParameterizedTest
@MethodSource("userAndStringProvider")
public void testaddPost(User self, String msg) {
assumeTrue(self != null);
// Invariant:
assertInvariant(self);
// Pré-conditions:
// @requires msg != null;
assumeTrue(msg != null);
// Oldies:
Instant oldDate = Instant.now();
// Wait until oldDate is really old:
ArrayList<User> list = new ArrayList<User>(1);
while (!oldDate.isBefore(Instant.now())) {
list.add(self);
}
// old in:@ensures getPostNb() == \old(getPostNb()) + 1;
int oldPostNb = self.getPostNb();
// @ensures \old(lastIndex() >= 0) ==> nextIndex() == \old(nextIndex() + 1);
int oldNextIndex = self.nextIndex();
// @ensures \old(lastIndex() >= 0) ==> previousIndex() == \old(previousIndex() + 1);
int oldPrevIndex = self.previousIndex();
// @ensures \old(lastIndex() >= 0) ==> lastIndex() == \old(lastIndex() + 1);
int oldLastIndex = self.lastIndex();
// Exécution:
Post result = self.addPost(msg);
// Wait until exec date is really old:
Instant execDate = Instant.now();
while (!execDate.isBefore(Instant.now())) {
list.add(null);
}
list = null;
// Post-conditions:
// @ensures getPost(0).equals(\result);
assertEquals(self.getPost(0), result);
// @ensures \result.getText().equals(msg);
assertEquals(msg, result.getText());
// @ensures getPostNb() == \old(getPostNb()) + 1;
assertEquals(oldPostNb + 1, self.getPostNb());
// @ensures oldDate.isBefore(\result.getDate());
assertTrue(oldDate.isBefore(result.getDate()));
// @ensures \result.getDate().isBefore(Instant.now());
assertTrue(result.getDate().isBefore(Instant.now()));
if (oldLastIndex >= 0) {
// @ensures \old(lastIndex() >= 0) ==> nextIndex() == \old(nextIndex() + 1);
assertEquals(oldNextIndex + 1, self.nextIndex());
// @ensures \old(lastIndex() >= 0) ==> previousIndex() == \old(previousIndex() + 1);
assertEquals(oldPrevIndex + 1, self.previousIndex());
// @ensures \old(lastIndex() >= 0) ==> lastIndex() == \old(lastIndex() + 1);
assertEquals(oldLastIndex + 1, self.lastIndex());
} else {
// @ensures \old(lastIndex() == -1) ==> nextIndex() == \old(nextIndex());
assertEquals(oldNextIndex, self.nextIndex());
// @ensures \old(lastIndex() == -1) ==> previousIndex() == \old(previousIndex());
assertEquals(oldPrevIndex, self.previousIndex());
// @ensures \old(lastIndex() == -1) ==> lastIndex() == \old(lastIndex());
assertEquals(oldLastIndex, self.lastIndex());
}
// Invariant:
assertInvariant(self);
}
getPost的测试方法
public void testgetPost(User self, int i) {
assumeTrue(self != null);
// Invariant:
assertInvariant(self);
// Pré-conditions:
// @requires i >= 0 && i < getPostNb();
assumeTrue(i >= 0 && i < self.getPostNb());
// Save state for purity check:
saveState(self);
// Oldies:
// Exécution:
Post result = self.getPost(i);
// Post-conditions:
// @ensures \result.equals(getPosts().get(i));
assertEquals(self.getPosts().get(i), result);
// Assert purity:
assertPurity(self);
// Invariant:
assertInvariant(self);
}
isAfter/isBefore 方法
public boolean isAfter(Post p) {
return getDate().isAfter(p.getDate()); //getDate().isBefore(p.getDate())
}
PS。我不应该改变测试方法
您将新帖子添加到
listPosts
的末尾:
public Post addPost(String msg) {
listPosts.add(new Post(msg));
Index++;
return new Post(msg);
}
这意味着帖子按
dateCreation
升序排列(即先创建listPosts.get(0)
,然后是listPosts.get(1)
,然后是后面的帖子),这也意味着
self.getPost(i).isAfter(self.getPost(i + 1))
永远不可能是真的。您要么必须将该条件更改为
self.getPost(i).isBefore(self.getPost(i + 1))
或将新帖子添加到列表前面:
public Post addPost(String msg) {
listPosts.add(0, new Post(msg));
Index++;
return new Post(msg);
}
另请注意,
addPost()
创建了两个不同的Post
对象,这可能不是您想要的。你可能想要
public Post addPost(String msg) {
Post post = new Post(msg);
listPosts.add(0, post);
Index++;
return post;
}
对于带有消息“绑定必须为正”的
IllegalArgumentException
:
对 JDK17 源代码的搜索导致该消息有一个可能的原因:调用上限小于零的随机生成器。
由于您的代码不包含随机生成器的任何用法,因此不可能说出问题是什么。
也许当您包含该异常的堆栈跟踪时,可以帮助确定问题。