Writing unit tests for a Java class, I came across a method where there are external classes that I need to mock, as well as a static method inside the tested class that also needs to be mocked.
I’ve tried combining mock
as I’ve done for unit tests with “external” classes with mockStatic
but calculateSimulatedLiquidationPrice
is always outputing 0.0 (the default double value) no matter what implementation it has. What am I doing wrong?
Class to be tested:
public static double calculateSimulatedLiquidationPrice(ComposicaoSerieCurvaService priceCurveService,
ISuprimento supplyParcel, double step, StressParametros stressParams) throws IllegalArgumentException {
Double liquidationPrice;
Produto product = supplyParcel.getProduto();
LocalDate referenceDate = supplyParcel.getProduto().getDataSuprimentoIni();
try {
liquidationPrice = priceCurveService.getValorSeriePLD(referenceDate, product.getSubmercado(),
stressParams.getDataRefStress());
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
if (liquidationPrice != null) {
return liquidationPrice;
}
double submarketPremiumValue;
double simulatedForwardValue = calculateSimulatedForward(priceCurveService, supplyParcel, step, stressParams);
double submarketSwapValue = priceCurveService.getValorSWAPFonte(referenceDate, product.getFonte(),
stressParams.getDataRefStress());
double submarketForwardValue = simulatedForwardValue - submarketSwapValue;
try {
submarketPremiumValue = priceCurveService.getValorAgioByDate(referenceDate, product.getSubmercado(),
stressParams.getDataRefStress());
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
return submarketForwardValue - submarketPremiumValue;
}
Test class:
@Test
public void testCalculateSimulatedLiquidationPriceSuccessSubmarketValue() throws Exception {
ComposicaoSerieCurvaService priceCurveService = mock(ComposicaoSerieCurvaService.class);
ISuprimento supplyParcel = mock(ISuprimento.class);
Produto product = mock(Produto.class);
CurvasSimuladasParametros stressParams = mock(CurvasSimuladasParametros.class);
Preco price = new Preco();
price.setPreco(PrecoEnum.F);
Mockito.when(supplyParcel.getPreco()).thenReturn(price);
Mockito.when(supplyParcel.getProduto()).thenReturn(product);
Mockito.when(supplyParcel.getProduto().getDataSuprimentoIni()).thenReturn(LocalDate.now());
Mockito.when(priceCurveService.getValorSeriePLD(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(null);
Mockito.when(priceCurveService.getValorSWAPFonte(Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(10.0);
Mockito.when(priceCurveService.getValorAgioByDate(Mockito.any(), Mockito.any(), Mockito.any()))
.thenReturn(10.0);
try (MockedStatic<PriceSimulator> priceSimulatorMock = Mockito.mockStatic(PriceSimulator.class)) {
priceSimulatorMock
.when(() -> PriceSimulator.calculateSimulatedForward(Mockito.any(),
Mockito.any(), Mockito.anyDouble(),
Mockito.any()))
.thenReturn(30.0);
double output = priceSimulator.calculateSimulatedLiquidationPrice(priceCurveService, supplyParcel, 10.0,
stressParams);
Assertions.assertEquals(10.0, output, ACCEPTABLE_ERROR);
}
}