Junit测试失败,但我的方法在Java赋值中似乎很好

6ljaweal  于 12个月前  发布在  Java
关注(0)|答案(2)|浏览(79)

我正在做一个作业,下周就要交了,所以基本上我有一个类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) {
    Post post = new Post(msg);
    listPosts.add(0, post);
    Index++;
    return post;
}


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: <true> but was: <false>,当查看测试的类时,我注意到它在这一点上失败了

for (int i = 0; i < (self.getPostNb() - 1); i++) { //getPostNb() = listPost.size()
        assertTrue(self.getPost(i).isAfter(self.getPost(i + 1)));
    }


对于getPost方法,即使没有运行测试,它也会给出错误error java.lang.IllegalArgumentException: bound must be positive
有什么建议吗?谢谢。
我需要Junit测试都返回成功,但只有addPost和getPost失败,这使得使用它们的其他两个方法也被中止
编辑:assertInvariant方法

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())
}


附言。我不应该改变测试方法
更新:测试数据由

public static Stream<Arguments> userAndStringProvider() {
    return Stream
            .generate(() -> Arguments.of(userSupplier(), stringSupplier()))
            .limit(LG_STREAM);
}

public static List<User> allUser() {
    return Collections.unmodifiableList(allUsers);
}

public static User userSupplier() {
    return getRandomElt(allUsers);
}

public static <T> T getRandomElt(Collection<T> c) {
    int index = randInt(c.size());
    if (c instanceof List<?>) {
        return ((List<T>) c).get(index);
    }
    int i = 0;
    for (T elt : c) {
        if (i == index) {
            return elt;
        }
        i++;
    }
    throw new NoSuchElementException();
}

moiiocjp

moiiocjp1#

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
对JDK 17源代码的搜索导致了该消息的一个可能原因:对上限小于零的随机生成器的调用。
因为你的代码不包含任何随机生成器的用法,所以不可能说问题出在哪里。
也许当你包括那个异常的堆栈跟踪时,它可以帮助你确定问题。

o4hqfura

o4hqfura2#

所以最近我和我的教授开会,给他看了这个错误,这是答案显然我应该有3个索引而不是1个,这就是为什么测试失败了唉,当我做了改变,测试成功了

public Post addPost(String msg) {
        Post post = new Post(msg);
        listPosts.add(0, post);
        previousIndex++; //index of the previous post
        nextIndex++;     //index of the next post
        lastIndex++;     //index of the last post visited (or current post)
        return post;
    }

字符串
对于getPost,这是正确的,我想感谢所有参与帮助我解决这个问题的人,我祝你们今天休息愉快,谢谢,再见

相关问题