0

我试图模拟一个非常简单的代码行,用于使用 Java 查询 DynamoDB。这是查询的一些示例代码 -

List<Pojo> result;
        try {
            if (filters == null) {
                this.queryExpression = new DynamoDBQueryExpression<Pojo>()
                        .withKeyConditionExpression(partitionKeyCondition)
                        .withExpressionAttributeValues(this.eav);
            } else {
                setFilterQueryExpression(filters);
            }
            result = this.dynamoDBMapper.query(Pojo.class, queryExpression);
        } catch (final Exception e) {
            throw new InternalServerException("Something went wrong with the database query: ", e);
        }

上面的代码有效,我能够检索自动反序列化到 Pojo 中的行列表。

我现在正在尝试this.dynamoDBMapper.query按如下方式模拟通话 -

@Mock
private DynamoDBMapper mapper;
List<Pojo> result = new ArrayList<>();

when(mapper.query(Pojo.class,Mockito.any(DynamoDBQueryExpression.class)).thenReturn(result);

我无法做到这一点 -

Cannot resolve method 'thenReturn(java.util.List<com.amazon.xxx.xxx.Pojo>)'

我还尝试了另一种方法-

doReturn(result).when(mapper).query(Pojo.class, Mockito.any(DynamoDBQueryExpression.class));

这似乎可以编译,但测试失败并出现错误 -

org.mockito.exceptions.misusing.WrongTypeOfReturnValue 

我查看了查询的预期输出为 type 的其他示例PaginatedQueryList,我也尝试过更改。但我仍然不确定为什么上面会引发错误。

4

2 回答 2

0

事实证明,.thenReturn为了完成该when部分,您之前缺少一个括号。一旦你添加它并从返回类型切换ListPaginatedQueryList,它应该编译。另请注意,这any是一个匹配器。一旦你指定了一个匹配器,所有的参数都需要是匹配器,因此使用eqetc. 作为你的 Pojo 类型。否则,Mockito 将InvalidUseOfMatchersException在运行时显示。这是一个对我有用的简化示例:

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression;
import com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MockTest {

    @Test
    void test() {
        DynamoDBMapper mapperMock = mock(DynamoDBMapper.class);
        PaginatedQueryList<String> expected = mock(PaginatedQueryList.class);
        // Note that when needs to be completed before thenReturn can be called.
        when(mapperMock.query(eq(String.class), Mockito.any(DynamoDBQueryExpression.class))).thenReturn(expected);

        QueryService queryService = new QueryService(mapperMock);
        PaginatedQueryList<String> actual = queryService.query();

        assertEquals(expected, actual);
    }

    public class QueryService {
        private final DynamoDBMapper mapper;

        public QueryService(DynamoDBMapper mapper) {
            this.mapper = mapper;
        }

        public PaginatedQueryList<String> query() {
            DynamoDBQueryExpression<String> queryExpression = new DynamoDBQueryExpression<>();
            return mapper.query(String.class, queryExpression);
        }
    }
}
于 2022-01-30T18:52:16.337 回答
0

使用 ArgumentMatchers 时是否也会出现错误?

Mockito.when(mapper.query(ArgumentMatchers.any(Pojo.class),ArgumentMatchers.any(DynamoDBQueryExpression.class)).thenReturn(result));

如果您(临时)扩展 ArgumentMatchers,您是否也会收到错误消息?

Mockito.when(mapper.query(ArgumentMatchers.any(),ArgumentMatchers.any()).thenReturn(result));
于 2022-01-27T22:13:22.330 回答