Case: I have a class which uses Autowired on two different classes objects. I have to unit test methods of this class but the methods uses calling to object's methods to perform some operation inside those methods that i want to test.
Therefore, i need to mock the calling of those methods and return something that mock the values and test the rest of the operation. What dived me into struggle that whenever i try to mock those field injection it throws me some sort of error related to Mock method invocation.
Solution:
Firstly, we need to annotate the test class with @RunWith(MockitoJUnitRunner.class) this annotation will help us to initialize the fields annotated with @Mock annotations.
Also we need to use the MockitoAnnotations.initMocks(this) though we can use any of them but in my case it does not seem to be working when removing the @RunWith annotation it throws me Assertion Error (it can be because of the fact that i am testing a VOID function by try catch and Assert.assertTrue). However, the main purpose of the @RunWith annotation is keep tests clean and improves debugging experience.
Second step is to Mock the declaration of the Autowired class attributes and use @InjectMocks on the class that contains the methods you want to mock. (@InjectMocks is used on the field which injection should be performed your private fields)
Third step is to use MockitoAnnotations.initMocks(this) in @Before method. Also, you can specify the mocking logic in this method if the logic repeats itself.
Fourth is to Mock the object's method call such as Mockito.when(firstMockedObject.someMethod(anyArguments)).thenReturn(yourMockedObject)
for e.g see: dzone.com/articles/use-mockito-mock-autowired
We generally use mock when we want to mock whole object's behavior while using spy we will be spying or stubbing specific methods of it. So Mock acheives full mocking and Spy acheives partial mocking.
EXPECTED EXCEPTION
when declaring the ExpectedException it should be declared as public otherwise it won't work
AssumptionViolationException
An exception class used to implement assumptions. Assumption is a state in which a given test is meaningful and should or should not be executed. A test for which an assumption fails should not generate a test case failure.
Therefore, i need to mock the calling of those methods and return something that mock the values and test the rest of the operation. What dived me into struggle that whenever i try to mock those field injection it throws me some sort of error related to Mock method invocation.
Solution:
Firstly, we need to annotate the test class with @RunWith(MockitoJUnitRunner.class) this annotation will help us to initialize the fields annotated with @Mock annotations.
Also we need to use the MockitoAnnotations.initMocks(this) though we can use any of them but in my case it does not seem to be working when removing the @RunWith annotation it throws me Assertion Error (it can be because of the fact that i am testing a VOID function by try catch and Assert.assertTrue). However, the main purpose of the @RunWith annotation is keep tests clean and improves debugging experience.
Second step is to Mock the declaration of the Autowired class attributes and use @InjectMocks on the class that contains the methods you want to mock. (@InjectMocks is used on the field which injection should be performed your private fields)
Third step is to use MockitoAnnotations.initMocks(this) in @Before method. Also, you can specify the mocking logic in this method if the logic repeats itself.
Fourth is to Mock the object's method call such as Mockito.when(firstMockedObject.someMethod(anyArguments)).thenReturn(yourMockedObject)
for e.g see: dzone.com/articles/use-mockito-mock-autowired
We generally use mock when we want to mock whole object's behavior while using spy we will be spying or stubbing specific methods of it. So Mock acheives full mocking and Spy acheives partial mocking.
EXPECTED EXCEPTION
when declaring the ExpectedException it should be declared as public otherwise it won't work
AssumptionViolationException
An exception class used to implement assumptions. Assumption is a state in which a given test is meaningful and should or should not be executed. A test for which an assumption fails should not generate a test case failure.
No comments:
Post a Comment