Question on MockConstruction

557 views
Skip to first unread message

Sylvain Archenault

unread,
Oct 29, 2020, 7:05:23 PM10/29/20
to mockito
Hi,

I started using mockConstruction and it works well, but i'm wondering if there is an easier way to do what i'm doing. I atteched a whole test - but it comes down to this code:

Car car1 = mock(Car.class);
Car car2 = mock(Car.class);
MockedConstruction<Car> carMockedConstruction = Mockito.mockConstruction(Car.class, context -> {
Car car = context.arguments().get(0).equals("1") ? car1 : car2;
return Mockito.withSettings().defaultAnswer(i -> i.getMethod().invoke(car, i.getArguments()));
});


So basically, based on constructor argument, I want to return different mock, this part is fine, but is there a cleaner way to do  "Mockito.withSettings().defaultAnswer(i -> i.getMethod().invoke(car, i.getArguments()));
});" 

It works, but it doesnt' seem very natural.

Thank you for the great library !

UnitTest.java
Reply all
Reply to author
Forward
0 new messages