sdk-junit的spring-mock-listusersiterable

uqzxnwby  于 2021-07-16  发布在  Java
关注(0)|答案(1)|浏览(320)

在我的服务类方法中,我有一段代码需要测试

List<UserType> users = new ArrayList<>();
    ListUsersIterable responses = cognitoclient.listUsersPaginator(ListUsersRequest
                                    .builder()
                                    .userPoolId(userUserPoolId)
                                    .build());
    for (ListUsersResponse response : responses) {
        users.addAll(response.users());
    }

嘲笑的时候 cognitoclient.listUsersPaginator() 我无法创建listusersiterable的存根,因为没有可用的setter方法。
下面是我正在使用的aws依赖项

implementation platform('software.amazon.awssdk:bom:2.15.0')
implementation 'software.amazon.awssdk:cognitoidentityprovider'
implementation 'software.amazon.awssdk:sts'
ercv8c1e

ercv8c1e1#

找到了解决办法。

List<AttributeType> userAttributesList = new ArrayList<>();  
userAttributesList.add(AttributeType.builder().name("custom:status").value("active").build());

ListUsersIterable mockUsersIterable = mock(ListUsersIterable.class);        
when(cognitoclient.listUsersPaginator(any(ListUsersRequest.class))).thenReturn(mockUsersIterable);
List<ListUsersResponse> list = new ArrayList<>();
List<UserType> userTypeList = new ArrayList<>();
userTypeList.add(UserType.builder().attributes(userAttributesList).build());
list.add(ListUsersResponse.builder().users(userTypeList).build());
when(mockUsersIterable.iterator()).thenReturn(list.iterator());

相关问题