Setup
Maven Dependencies: Mockito's BDD flavour is included in the mockito-core library, so all we need to do is include the artefact:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.21.0</version>
</dependency>
Imports: If we add the following static import to our tests, they will become more readable:
import static org.mockito.BDDMockito.*;
Mockito VS BDDMockito
Mockito's traditional mocking is done with when(obj).then*() in the Arrange step.
Later, in the Assert step, we can use verify() to validate interaction with our mock.
Because BDDMockito provides BDD aliases for various Mockito methods, we can write our Arrange step with given (rather than when), and our Assert step with then (instead of verify).
Consider the following example of a test body created with traditional Mockito:
when(phoneBookRepository.contains(momContactName))
.thenReturn(false);
phoneBookService.register(momContactName, momPhoneNumber);
verify(phoneBookRepository)
.insert(momContactName, momPhoneNumber);
Let's see how it stacks up against BDDMockito:
given(phoneBookRepository.contains(momContactName))
.willReturn(false);
phoneBookService.register(momContactName, momPhoneNumber);
then(phoneBookRepository)
.should()
.insert(momContactName, momPhoneNumber);
BDD example
Create a CalculatorService interface to provide mathematical functions.
CalculatorService.java
public interface CalculatorService {
public double add(double input1, double input2);
public double subtract(double input1, double input2);
public double multiply(double input1, double input2);
public double divide(double input1, double input2);
}
To represent MathApplication, create a JAVA class.
MathApplication.java
public class MathApplication {
private CalculatorService calcService;
public void setCalculatorService(CalculatorService calcService){
this.calcService = calcService;
}
public double add(double input1, double input2){
return calcService.add(input1, input2);
}
public double subtract(double input1, double input2){
return calcService.subtract(input1, input2);
}
public double multiply(double input1, double input2){
return calcService.multiply(input1, input2);
}
public double divide(double input1, double input2){
return calcService.divide(input1, input2);
}
}
Let's put the MathApplication class to the test by injecting a calculatorService mock into it. Mockito will be the one to create Mock.
MathApplicationTester.java
package com.tutorialspoint.mock;
import static org.mockito.BDDMockito.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
// @RunWith attaches a runner with the test class to initialize the test data
@RunWith(MockitoJUnitRunner.class)
public class MathApplicationTester {
private MathApplication mathApplication;
private CalculatorService calcService;
@Before
public void setUp(){
mathApplication = new MathApplication();
calcService = mock(CalculatorService.class);
mathApplication.setCalculatorService(calcService);
}
@Test
public void testAdd(){
//Given
given(calcService.add(20.0,10.0)).willReturn(30.0);
//when
double result = calcService.add(20.0,10.0);
//then
Assert.assertEquals(result,30.0,0);
}
}
To execute Test case(s), create a java class file named TestRunner in C:\> Mockito WORKSPACE.
TestRunner.java
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(MathApplicationTester.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Compile the classes using javac compiler to verify the result:
C:\Mockito_WORKSPAE>javac CalculatorService.java MathApplication.
java MathApplicationTester.java TestRunner.java
To see the result, run the Test Runner.
C:\Mockito_WORKSPACE>java TestRunner
Verify the output.
true
FAQs
-
What is <T> given(T methodCall)?
It's very similar to the method when(TmethodCall). It allows for stubbing.
-
What is <T> then(T mock)?
It enables mock behaviour verification in the BDD style.
-
What is BDDStubber will(Answer<?> answer)?
It's similar to the method doAnswer(Answer answer). When we want to stub a void method with the generic Answer, we use this.
-
What is BDDStubber willReturn(Object toBeReturned)?
It's similar to the doReturn(Object toBeReturned) method in that it returns an object. It's used in place of when you don't know what to say (Object).
-
What is BDDStubber willThrow(Class<? extends Throwable> toBeThrown)?
It works in the same way as the doThrow(Class<? extends Throwable> toBeThrown) method. When we want to stub a void method with an exception, we use it.
Key Takeaways
In this article, we have extensively discussed the theoretical and practical implementation of Mockito BDD.
We hope that this blog has helped you enhance your knowledge regarding the theoretical and practical implementation of Mockito BDD and if you would like to learn more, check out our articles on Coding Ninjas Studio. Do upvote our blog to help other ninjas grow. Happy Coding!