Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass constructor arguments using Mockito

Tags:

java

mockito

I want to mock a class using the Mockito framework, that has a couple of constructor arguments.

How would i go about passing those constructor arguments without generating setters for the private member variables ?

Thanks

like image 743
Dante Avatar asked Oct 06 '22 15:10

Dante


1 Answers

You said you want to mock some but not all of the methods. I'm not sure why you'd want to do this - if your class is a collaborator, then it would make sense to mock the whole class. Or if it's the SUT, you probably don't want to mock it at all.

It's possible that what you're looking for is a spy, rather than a mock. If you decide to use a spy, you'll make it from a real object, which has already been constructed using whatever arguments you need it to be constructed from.

But before you consider using a spy, I urge you to think more carefully about exactly what it is that you're testing, and why you think you need to replace some of your methods with mock implementations, but not others.

like image 158
Dawood ibn Kareem Avatar answered Oct 10 '22 03:10

Dawood ibn Kareem