有一個用戶可以訪問的頁面串列。有一個函式根據verifyAccess用戶嘗試訪問的頁面回傳真/假。
例如 - 在個人資料頁面上查找個人資料 IMG 定位器,在注銷頁面上查找注銷按鈕,等等......
我正在使用以下匯入(JUnit API)
import org.assertj.core.api.SoftAssertions;
import org.junit.Assert;
import org.junit.jupiter.api.function.Executable;
像這樣
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();
}
}
問題是assertList串列大小始終為 1,但for回圈運行 12 次,共 12 頁。也assertAll給出以下錯誤java.lang.ClassCastException: java.util.stream.ReferencePipeline$Head cannot be cast to org.junit.jupiter.api.function.Executable
我在這里做錯了什么?
uj5u.com熱心網友回復:
問題是 assertList 串列大小始終為 1,但 for 回圈運行 12 次,共 12 頁。
for 回圈的每次運行都會assertList再次初始化變數。因此,它始終只包含一個元素。
要斷言您的每個結果,verifyAccessToPage可以使用 AssertJ 的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();
}
});
相反,如果您想測驗沒有呼叫verifyAccessToPage引發例外,您可以將代碼更改為:
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);
uj5u.com熱心網友回復:
您可以使用以下庫:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.oneOf;
然后像這樣斷言:
assertThat(assertList, is(oneOf(expectedBooleanValues)));
expectedBooleanValues 可以是 true 或 false
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/459103.html
上一篇:如何引數化定位器?
