如何使用mockito模拟返回未来java对象的方法

时间:2018-05-31 06:08:49

标签: java spring unit-testing mocking mockito

我有一个方法验证,我需要编写junit测试用例

public List<Field> validate(){

        final Future<Map<ObjectId, Field>> uniqueResult = globalUniqValidator.execute(uniqueValidations,
                validationContent.getEvalFieldValueMap(), inputFieldValuesMap);

        final Future<Map<ObjectId, Field>> subnetResult = globalSubnetValidator.execute(subnetValidations,
                validationContent.getEvalFieldValueMap(), inputFieldValuesMap, surveyTemplateRefId);
}

它有两个返回未来对象的方法

@RunWith(MockitoJUnitRunner.class)
public class GlobalValidationServiceTest {
    @Mock
    private Future<Map<ObjectId, Field>> future;

    @Test
    public void validateGlobalValidation() throws Exception {

    given(globalUniqValidator.execute(uniqueValidations, validationContent.getEvalFieldValueMap(), inputFieldValuesMap)).willReturn(future);
    when(future.get()).thenReturn(inputFieldValuesMap);



    given(globalSubnetValidator.execute(globalValidation, evalFieldValueMap, inputFieldValuesMap, surveyTemplateRefId)).willReturn(future);
    when(future.get()).thenReturn(inputFieldValuesMap_subnet);


    }

然而,当我调试时,我获得了uniqueResult和subnetResult的null 我猜它没有被嘲笑

它还表示在模拟写入的行中未使用的模拟

我错过了什么吗?

0 个答案:

没有答案