Mocking a class means that a virtual class is created based upon the original, and that class is rendered to a variable.
This new class will have behaviours how the class will act for actions coming from caller side. Practically, a behaviour equals with calling a method which name is identical to one of the original method names of the class.
Mocking is important in testing with which making independent unit tests become possible.
In case in a testing environment emulated classes are needed (e.g. file environment), mocking is the best.
Mockito is one of the mocking toolsets.
The Spring Test dependency includes Mockito dependency.
In order to address Mockito, the following tags should be inserted into pom.xml.
After modifying the pom.xml, in IntelliJ IDEA, right-click on the pom.xml and select the Maven/Reload Project menu item.
Alternative way is to type "mvn compile" to make Maven to find and install all what is needed.
EXAMPLE FOR ADDING MOCKITO DEPENDENCY:
Mocking a variable means the variable will act like what references to a real memory or disk allocation but instead, will be held in memory and limited to keep its data as needed for mocking.
Behavior means how the variable will act for calling certain methods, with this, will emulate a true behavior of corresponding class.
Some formulas for emulating methods (setting up behaviors):
when(mockedVariable.methodWithReturnValue()).thenReturn(value);
when(mockedVariable.methodWithReturnValue()).thenThrow(exception);
doThrow(exception).when(mockedVariable).methodWithoutReturnValue();
doNothing().when(mockedVariable).methodWithoutReturnValue();
The thenReturn() method can not receive mocked class in its argument.
Practically, any class can be mocked.
EXAMPLE FOR MOCKING (GENERAL SOLUTION):
EXAMPLE FOR MOCKING (FILE 1):
EXAMPLE FOR MOCKING (FILE 2):
MORE HERE: https://www.vogella.com/tutorials/Mockito/article.html