In this article, we will see some example of @Mock
annotation. If your tests are dependent on many mock objects then @Mock
annotation comes very handy. It is explicit and makes the test class more readable.
Mockito is an open source mock unit testing framework for Java. In this article, we will look into some of the stubbing examples using answer callback.
Below are my setup details:
- I am using Maven – the build tool
- Eclipse as the IDE, version Luna 4.4.1.
- TestNG is my testing framework.
- Add Mockito dependency to our
pom.xml
.
Dependencies
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javarticles.mockito</groupId> <artifactId>mockitoVerifyBehavior</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>2.0.5-beta</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
System Under Test
All our stubbing examples follow a simple model. Our model consists of interface Tree and class Seed. We know when we plant a seed, it eventually grows into a tree. We have included this in Tree interface.
Tree:
package com.javarticles.mokcito; public interface Tree { Tree grow(Seed seed); void growSelf(Seed seed); }
Seed:
package com.javarticles.mokcito; public class Seed { private String name; private boolean isRotten; private Tree tree; public Seed(String name) { this.name = name; } public Tree grow() { if (!isRotten() && tree != null) { return tree.grow(this); } return null; } public boolean equals(Object o) { if (o == null) { return false; } if (!(o instanceof Seed)) { return false; } Seed seed = (Seed) o; return name.equals(seed.getName()); } public String getName() { return name; } public String toString() { return "Seed(" + name + ")"; } public boolean isRotten() { return isRotten; } public void setRotten(boolean isRotten) { this.isRotten = isRotten; } public void setTree(Tree tree) { this.tree = tree; } }
Using @Mock Annotation
Before the test method is invoked, we need to make sure that MockitoAnnotations.initMocks(this)
so that the fields are annotated with @Mock
are initialized with the mock object.
MockAnnotationTests:
package com.javarticles.mockito; import static org.mockito.Matchers.any; import static org.mockito.Mockito.verify; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.javarticles.mokcito.Seed; import com.javarticles.mokcito.Tree; public class MockAnnotationTests { @Mock private Tree tree; @BeforeMethod public void initMock(){ MockitoAnnotations.initMocks(this); } @Test public void callMethodsOnMock() { tree.grow(new Seed("Apple")); verify(tree).grow(any(Seed.class)); } }
MockitoJUnitRunner Example
Running the test suite with MockitoJUnitRunner
will initialize the fields annotated with @Mock
. It is compatible with JUnit 4.4 and higher. Mocks are initialized before each test method.
MockAnnotationJUnitDrivenTests:
package com.javarticles.mockito; import static org.mockito.Matchers.any; import static org.mockito.Mockito.verify; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; import com.javarticles.mokcito.Seed; import com.javarticles.mokcito.Tree; @RunWith(MockitoJUnitRunner.class) public class MockAnnotationJUnitDrivenTests { @Mock private Tree tree; @Test public void callMethodsOnMock() { tree.grow(new Seed("Apple")); verify(tree).grow(any(Seed.class)); } }
If you are using TestNG, you need to annotate with @Listeners(MockitoTestNGListener.class)
.
Download the source code
This was an example about Mockito @Mock annotation.