Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying mocked object method parameters

Tags:

java

mocking

public class Card
{

public double roundMoney(double currencyValue , int digits) {
//some logic for rounding which includes heavy service calling
} 

}

Now I want to mock the class and whenever the roundMoney methods is called with parameters I want it to return the product of the parameters

Card mycard = mock(Card.class);
when(mycard.roundMoney(anyDouble(), anyInt()).thenReturn( anyDouble() * anyInt() ));

How can I achieve this?

like image 784
Joel Avatar asked Apr 09 '26 03:04

Joel


1 Answers

Use thenAnswer instead of thenReturn? The answer implementation has access to the parameter values being passed.

See: Making a mocked method return an argument that was passed to it

like image 93
dev101 Avatar answered Apr 10 '26 16:04

dev101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!