Suppose you have a class with a public constructor. This class contains a private object that do a lot of stuff you don’t want it to do for unit testing. Here’s an excellent way to do that.
Imagine you have a User class that contains a class for authentication:
1 2 3
You could have a constructor that looks like this:
1 2 3 4
Uh-oh! How can we stop user from using Authenticator class so we can make real unit tests? I’ve searched a lot, but in the end, I believe the best way to do this is to add a little bit of code pollution. You have to add a protected constructior like this:
1 2 3 4
And you are done! Just add a little comment mentionning that this constructor is only to be used for unit testing purposes, and the tests will go straight forward with JUnit and Mockito:
1 2 3 4 5 6
Notice the UserMock thing. No, it’s not a User, you are right! We use the protected constructor to extend User in our UserTest file.
1 2 3 4 5
I hope this will help!