Reputation: 163
I need to learn Mockito for my current project and am struggling mightely. I might add that i've also never been taught anything on unit testing either (and very little in java for that mater, haha) so I seem to have to learn multiple things at once. I have spent awhile looking at a number of resources on Mockito but could still use some help. Below I have created a small example that I'm hoping someone can answer for me on creating a JUNIT for it in Mockito.
public class Animal{
public numberOfTeeth(AnimalType animal){
if animalAlive(){
Teeth tooth= animal.getNumberOfTeeth();
if(tooth== null) {
throw new ValidationException("Invalid Tooth");
}
}
}
Please excuse the syntax errors, this is a quick made up example for learning.
So lets say animalAlive() is a private method in the Animal Class and getNumberOfTeeth() is a public method in some other class. I assume one junit test one might make would be to test if the exception is thrown when it should? What would the best way to do this be?
I would assume it involves mocking AnimalType and creating stubs(?) to control the two method calls inside the numberOfTeeth method? Does the Private method inside the Animal class throw a wrench in anything?
Upvotes: 0
Views: 310
Reputation: 79838
To be entirely thorough in your testing of this method, I'd say you want test cases for the following three scenarios.
So you'll need the following.
Animal
object to test,AnimalType
,Tooth
object, which may or may not be a mock,ExpectedException
rule - there are other ways of testing that exceptions are thrown, but this is the most versatile, so I recommend learning to use it now, and using it in all of your tests that involve exceptions, even if it seems overkill.In your AnimalTest
class, you'll have three test methods, one for each of the scenarios that you're going to test.
numberOfTeethFailsForLiveAnimalWithInvalidTeeth
ValidationException
.numberOfTeeth
on a live animal, with your mock AnimalType
. Since you haven't stubbed getNumberOfTeeth()
, it will return null, so the exception should be thrown.numberOfTeethSucceedsForLiveAnimalWithValidTeeth
AnimalType
to return a valid Tooth
from getNumberOfTeeth()
.numberOfTeeth
on a live animal, with your mock AnimalType
.There is no need to verify anything - the fact that this method completes means the exception is not thrown.
numberOfTeethSucceedsForDeadAnimal
numberOfTeeth
on a live animal, with your mock AnimalType
.Again, there is no need to verify anything - the fact that this method completes means the exception is not thrown.
There are a few small things you can do to improve your code.
AnimalType
parameter animal
, when you have a type called Animal
- it's just too confusing.getNumberOfTeeth()
method - it doesn't return a number, it returns a Tooth
, so this name is also confusing.Upvotes: 2