Introduction
In software development, one gets to test its application whether it is expected to do so or not. For that reason, Mockito provides a Timeout option that may be used to see if a method is called within a certain amount of time.
So let's get started with writing our timeout in mockito.
Getting started
Assuming that you are familiar with the project setup in maven. Let see the practical example
1. Create a CalculatorService interface to offer mathematical functions.
public interface CalculatorService {
public double add(double value1, double value2);
public double subtract(double value1, double value2);
public double multiply(double value1, double value2);
public double divide(double value1, double value2);
}
2. Create a java class, MathApplication.
public class MathApplication {
private CalculatorService calcService;
public void setCalculatorService(CalculatorService calcService) {
this.calcService = calcService;
}
public double add(double value1, double value2) {
return calcService.add(value1, value2);
}
public double subtract(double value1, double value2) {
return calcService.subtract(value1, value2);
}
public double multiply(double value1, double value2) {
return calcService.multiply(value1, value2);
}
public double divide(double value1, double value2) {
return calcService.divide(value1, value2);
}
}
3. Test the MathApplicationTester class.Let's put the MathApplication class to the test by injecting a calculatorService mock into it. Mockito will be the one to make Mock.
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
// To initialise the test data, @RunWith connects a runner to the test class.
@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 testAddAndSubtract() {
// add the behaviour to add numbers
when(calcService.add(20.0, 50.0)).thenReturn(70.0);
// subtract the behaviour to subtract numbers
when(calcService.subtract(50.0, 10.0)).thenReturn(40.0);
// test the subtract functionality
Assert.assertEquals(mathApplication.subtract(50.0, 20.0), 30.0, 0);
// test the add functionality
Assert.assertEquals(mathApplication.add(50.0, 10.0), 60.0, 0);
// ensure that the call to add method is performed in less than 500 milliseconds.
verify(calcService, timeout(500)).add(20.0, 10.0);
// to ensure that multiplication invocations can be examined within a particular time window, an invocation count can be supplied.
verify(calcService, timeout(100).times(1)).subtract(20.0, 10.0);
}
}
4. Create a file name MathApplicationTester.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());
}
}
5. In the terminal, execute this command.
javac CalculatorService.java MathApplication.java MathApplicationTester.java TestRunner.java
6. Execute the java file.
java TestRunner





