Home > Back-end >  assertAll a list of booleans
assertAll a list of booleans

Time:04-19

There is a list of pages that can be accessed by a user. The verifyAccess has a function that returns true/false depending on which page the user is trying to access.

For example - look for the profile IMG locator on a profile page, the Logout button on the logout page, and so on ...

I am using below imports (JUnit APIs)

import org.assertj.core.api.SoftAssertions;
import org.junit.Assert;
import org.junit.jupiter.api.function.Executable;

Like this

List<Boolean> assertList = null;
 for (int i = 0; i < pageList.size(); i  ) {
            String pageName = pagesList.get(i);
            assertList = new LinkedList<>();
            assertList.add(accessCheck.verifyAccessToPage(userRole, pageName));
        }
    assertAll((Executable) assertList.stream()); 
}

public boolean verifyAccessToPage(String userRole, String pageName) {
        switch (page) {
            case "Profile page":
                return profilePage.profileImg.isCurrentlyEnabled();
            case "Create(invite) users":
                jobslipMyCompanyPage.clickInviteBtn();
                return logoutPage.logoutBtn.isCurrentlyEnabled();
                    }

}

Issue is assertList list size is always 1, but for loop run 12 times for 12 pages. Also assertAll is giving below error java.lang.ClassCastException: java.util.stream.ReferencePipeline$Head cannot be cast to org.junit.jupiter.api.function.Executable

What am I doing wrong here?

CodePudding user response:

You could use the following library:

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.oneOf;

and then assert like this:

assertThat(assertList, is(oneOf(expectedBooleanValues)));

expectedBooleanValues could be true or false

CodePudding user response:

Issue is assertList list size is always 1, but for loop run 12 times for 12 pages.

Each run of the for loop initializes the variable assertList again. Therefore, it always contains only one element.

To assert each result of verifyAccessToPage you could use AssertJ's SoftAssertions:

SoftAssertions.assertSoftly(soft -> {
    for (int i = 0; i < pageList.size(); i  ) {
        String pageName = pagesList.get(i);
        boolean accessToPage = verifyAccessToPage(userRole, pageName);
        soft.assertThat(accessToPage).as("access to page %s for role %s", pageName, userRole).isTrue();
    }
});

  • Related