Search code examples
javaunit-testingjunit

java.lang.NoClassDefFoundError: org/opentest4j/AssertionFailedError


Here is my function that I want to test:

@Override
public List<User> displayByDate(List<User> listUser) {
    if(listUser != null){
        Collections.sort(listUser, (User o1, User o2) -> o2.getUserDate().compareTo(o1.getUserDate()));
    }
    return listUser;
}

Here is the test case

public class UserImpTest {

UserImp userImp = new UserImp();

public UserImpTest() {
}

@Test
public void testDisplayByDate() {
    // Create sample user objects
    User user1 = new User(1, "John", new Date(122, 0, 15)); // January 15, 2022
    User user2 = new User(2, "Alice", new Date(123, 1, 10)); // February 10, 2023
    User user3 = new User(3, "Bob", new Date(121, 11, 1)); // December 1, 2021

    // Create a list of users in unsorted order
    List<User> userList = new ArrayList<>();
    userList.add(user1);
    userList.add(user2);
    userList.add(user3);

    // Create expected sorted list
    List<User> expectedList = new ArrayList<>();
    expectedList.add(user2); // Alice (latest date)
    expectedList.add(user1); // John
    expectedList.add(user3); // Bob (oldest date)

    // Call the displayByDate function
    List<User> sortedList = userImp.displayByDate(userList);

    // Assert the sorted list matches the expected list
    Assertions.assertEquals(expectedList, sortedList);
}

}

Here my .classpart file for my project enter image description here

I have tried to test several times but it still shows up an error java.lang.NoClassDefFoundError: org/opentest4j/AssertionFailedError

Does anyone know how to fix this?? (I am using NetBeans JDK 17)


Solution

  • It seems that your classpath is not having the jar for opentest4j and hence the class AssertionFailedError is not found at runtime. Maybe you can share the project structure including the .classpath file and pom.xml if it is maven project.