Category Archives: Mockito
Difference between BDD vs TDD in Software Approach
Mockito’s mock Methods
In this post, We will talk and learn about different overloaded mock() methods of org.mockito.Mockito class Mockito’s class org.mockito.Mockito provides below overloaded mock Methods: public static <T> T mock(Class<T> classToMock): It creates mock object of given class or interface. public static <T> T mock(Class<T> classToMock, String name) : In this method we can specify mock name.… Read More »
Mocking Exception Throwing using Mockito
In this post, We will learn How to throw an Exception using Mockito Let’s try to understand the above concept using a demo project pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>ExceptionThrowingUsingMockitoExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.5.7</version> <scope>test</scope> </dependency> </dependencies> </project> |
Dictionary.java
1 2 3 4 5 6 7 |
package com.kkjavatutorials; public interface Dictionary { public abstract void add(String word, String meaning); public abstract String getMeaning(String word); } |
WordDictionary.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.kkjavatutorials; import java.util.HashMap; import java.util.Map; public class WordDictionary implements Dictionary { private Map<String, String> wordsMap; public WordDictionary() { wordsMap = new HashMap<String, String>(); } public void add(String word, String meaning) { wordsMap.put(word, meaning); } public String getMeaning(String word) { return wordsMap.get(word); } } |
WordDictionaryTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.kkjavatutorials; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; import org.junit.jupiter.api.Test; /** * Method Exception Throwing testing using Mockito * @author KK JavaTutorials */ public class WordDictionaryTest { /** * Test for non-void Return Type method with Exception */ @Test public void nonVoidMethodThrowingExceptionMockitoTest() { try { WordDictionary wordDictionary = mock(WordDictionary.class); when(wordDictionary.getMeaning(anyString())) .thenThrow(NullPointerException.class); wordDictionary.getMeaning("awesome"); } catch (Exception e) { System.out.println("Exception occured!!"); assertTrue(e instanceof NullPointerException); } } /** * Test for void Return Type method with Exception */ @Test public void voidMethodThrowingExceptionMockitoTest() { WordDictionary wordDictionary = mock(WordDictionary.class); doThrow(new IllegalStateException("Error occurred!!")) .when(wordDictionary) .add(anyString(), anyString()); assertThrows(IllegalStateException.class, ()->wordDictionary.add("awesome", "Very Good")); } /** * Configuring Spy to throw an exception the same way we've done with the mock */ @Test public void nonVoidMethodThrowingExceptionWithSpyMockitoTest() { String emptyWord =""; try { WordDictionary wordDictionary = new WordDictionary(); WordDictionary spyWordDictionary = spy(wordDictionary); when(spyWordDictionary.getMeaning(emptyWord)) .thenThrow(new IllegalArgumentException("Input is not valid!!")); spyWordDictionary.getMeaning(emptyWord); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); assertEquals("Input is not valid!!",e.getMessage()); } } } |
The Output of the above project: You May Also Like: What is Mocking? Why Need for mocking? What… Read More »
Mocking Void Methods with Mockito
In this post, We will learn How to mock void methods with Mockito Usually most of the time Mockito’s org.mockito.stubbing.Stubber.when(T) method is really good enough to mock an object’s behavior but when we need to mock a void method then we can’t use when(T) method. Mockito Mock Void Method Mockito provides the following methods that can be… Read More »
Adding behavior to mocked object in Mockito
Mockito adds functionality to a mock object using the when() method. Take a look at the following code snippet.
1 2 3 4 |
MyUtils mock = mock(MyUtils.class); when(mock.add(10, 40)).thenReturn(50); doReturn(50).when(mock).add(10, 40); |
Here we are instructing the Mockito to provide or give a behavior of adding 10 and 40 to the add method of MyUtils and as a result, to return the value of 50 Let’s try to understand… Read More »
@Captor Annotation in Mockito with Example
In this post, We will talk and learn about @Captor Annotation in Mockito with Example Mockito ArgumentCaptor and @Captor Annotation Usually, Mockito’s ArgumentCaptor is used to capture arguments for mocked methods. ArgumentCaptor is used with Mockito to verify() methods to get the arguments passed when any method is called. This way usually, we can use additional JUnit assertions for our… Read More »
Mockito – Verifying Method Calls
We can use org.mockito.Mockito.verify(T mock) method to ensure whether a mock() method was called with required arguments or not. In other words, we can say that Mockito.verify(T mock) is used to confirm that specific interactions took place. Let’s try to understand the above concept using a demo project pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkjavatutorials</groupId> <artifactId>MockitoVerifyingMethodCallsExample</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <maven.compiler.target>8</maven.compiler.target> <maven.compiler.source>8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.5.7</version> <scope>test</scope> </dependency> </dependencies> </project> |
MyUtils.java
1 2 3 4 5 6 7 |
package com.kkjavatutorials; public interface MyUtils { public abstract int add(int n1,int n2); public abstract int multiply(int n1,int n2); } |
MyUtilsTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.kkjavatutorials; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import java.awt.List; public class MyUtilsTest { @Test void verify1Test() { MyUtils mock = mock(MyUtils.class); mock.add(10, 30); verify(mock).add(10, 30); verify(mock,times(1)).add(10, 30); } @Test void verify2Test() { MyUtils mock = mock(MyUtils.class); mock.add(10, 30); mock.add(10, 30); mock.add(10, 30); mock.add(10, 30); mock.add(10, 30); verify(mock,times(5)).add(10, 30); verify(mock,atLeast(2)).add(10, 30); verify(mock,atLeast(5)).add(10, 30); verify(mock,atLeastOnce()).add(10, 30); } @Test void verify3Test() { MyUtils mock1 = mock(MyUtils.class); MyUtils mock2 = mock(MyUtils.class); //mock1.add(10, 30); verifyNoInteractions(mock1,mock2); } @Test void verify4Test() { List mock = mock(List.class); mock.add("KK"); ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class); verify(mock).add(argumentCaptor.capture()); assertEquals("KK", argumentCaptor.getValue()); } } |
@InjectMocks Annotation in Mockito with Example
In this post, We will learn about @InjectMocks Annotation in Mockito with Example Mockito’s @InjectMocks annotation usually allows us to inject mocked dependencies in the annotated class mocked object. This is very useful when we have an external dependency in the class want to mock. We can specify the mock objects to be injected using @Mock annotation.… Read More »
@Mock and @Spy Mockito Annotations With Example
In this post, We will learn about @Mock and @Spy Mockito Annotations With Example? @ Mock Annotation The most Frequently used annotation in Mockito is @Mock Use @Mock annotation to create and inject mocked instances without having to call Mockito.mock(abc.class) manually. We may use org.mockito.Mockito class mock() method to create a mock object of a given class or interface. @Spy Annotation Use @Spyannotation to spy on… Read More »