可选用Mockito返回空对象

时间:2019-05-15 12:53:58

标签: java spring-boot mockito

我在测试用例中有以下代码,但是在测试用例中调用方法discountCurveReqMapper.getMapping时,它返回空的Optional对象而不是DiscountCurveReqMap对象

有人可以指导此处的问题吗

@RunWith(SpringRunner.class)
@SpringBootTest
public class IRSwapValuatorTest {


    @Autowired
    private IRSwapValuator valuator;

    @Mock
    private RestTemplate restTemplate;

    @Mock
    private YieldCurveProvider yieldCurveProvider;

    @Mock
    private ApplicationProperties applicationProperties;

    @MockBean
    private DiscountCurveReqMapper discountCurveReqMapper;

    @MockBean
    private SDPFixingInstrumentServiceImpl fis;

    @Before
    public void setUp() {

        .....
        .....

        DiscountCurveReqMap discountCurveReqMap = new DiscountCurveReqMap();
        when(discountCurveReqMapper.getMapping(any(String.class), any(String.class), any(Optional.class))).thenReturn(Optional.of(discountCurveReqMap));
        when(fis.load(any(String.class),any(String.class))).thenReturn(Optional.empty());
        this.valuator = new IRSwapValuator(restTemplate, applicationProperties, new SimpleMeterRegistry(), yieldCurveProvider, discountCurveReqMapper,fis);
    }

编辑:

getMapping方法的签名为:

public Optional<DiscountCurveReqMap> getMapping(@NotNull String currency,@NotNull  String type, Optional<String> settlement) {
        String key = currency + "_" + type + "_" + settlement.orElse("");
        return Optional.ofNullable(map.get(key));
    }

0 个答案:

没有答案