Issue
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?
Solution
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:
import static org.assertj.core.api.SoftAssertions.assertSoftly;
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();
}
});
If instead you want to test that no invocation of verifyAccessToPage
throws an exception, you could change the code to:
import static org.junit.jupiter.api.Assertions.assertAll;
List<Executable> assertList = new LinkedList<>();
for (int i = 0; i < pageList.size(); i++) {
String pageName = pagesList.get(i);
assertList.add(() -> accessCheck.verifyAccessToPage(userRole, pageName));
}
assertAll(assertList);
Answered By - Roland Weisleder
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.