javaarraylistjunitjunit5

Junit tests are failing but my methods seems fine in Java assignment


I'm working on an assignement that's due next week So basically I have a class User with the following attributes

private String userName, password;
private final Instant registrationDate;
private HashSet<User> setSubs;
private ArrayList<Post> listPosts;
private int Index;

The methods I'm trying to test are

public Post addPost(String msg) {
    Post post = new Post(msg);
    listPosts.add(0, post);
    Index++;
    return post;
}

and

public Post getPost(int i) {
    return listPosts.get(i);
}

Here's the class Post attributes

private final String textPost; 
private final Instant dateCreation; //this.dateCreation = Instant.now();
private HashSet<User> likedUsers;

Here's the issue I'm facing, when testing the method addPost it gives the error org.opentest4j.AssertionFailedError: expected: <true> but was: <false>, when looking at the test's class I noticed that its failing in this bit

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

And for the method getPost it gives the error error java.lang.IllegalArgumentException: bound must be positive whithout even running the test

Please any advice to fix them? thanks

I need the Junit tests to all return successful, but only addPost and getPost are failing which made two other methods that uses them to also be aborted

Edit: assertInvariant method

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

Test method for 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);
}

Test method for 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 method

public boolean isAfter(Post p) {
    return getDate().isAfter(p.getDate()); //getDate().isBefore(p.getDate())
}

PS. I shouldn't be changing the test methods

UPDATE: the test data is provided by

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

Solution

  • so recently I was in a meeting with my professor, and showed him this error and here's the answer Apparently I'm supposed to have 3 indexes instead of one, and that's what have been messing with th tests causing them to fails Alas, when I did the changes, the tests were successful

        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;
        }
    

    And for getPost, it was correct this whole time I want to thank everyone that participated in helping me solve this issues, and I wish you all a good rest of day Thanks and Goodbye