What is Unit Testing?
Unit Testing is a method of testing the smallest piece of code called a unit. The main aim is to isolate each unit of the system to identify, analyze and fine-tune the defects.
What are the Benefits of Unit Testing?
- Reduces bugs when transmuting the existing functionality.
- Reduces the Cost of Testing as defects are captured in the very early phase.
- Ameliorates design and sanctions better refactoring of code.
What is the Mockito framework?
Mockito is a mocking framework. It is a Java-predicated library used to engender simple and basic test APIs for performing unit testing of Java applications. It can additionally be utilized with other frameworks such as JUnit and TestNG.
What is mocking?
Mocking is primarily utilized in unit testing. An object under test may have dependencies on other objects. To isolate the behavior of the object we optate to supersede the other objects by mocks that simulate the behavior of the real objects. This is utilizable if the genuine objects are impractical to incorporate into the unit test.
In short, mocking is engendering objects that simulate the behavior of real objects.
What are the steps to be performed while using the Junit with Mocking framework?
- Initialize required objects for working with mocks and tested method
- Set the mock behavior on dependent objects
- Execute the tested method
- Perform assertions
- Verify if a method is invoked or not
What is the use of the mock() method?
The Mock() method is used to create and inject the mocked instances. The other way of creating the instances is using the @mock annotations.
Difference between Assert and Verify?
Assert: If the assert condition is true then the program control will execute the next test step but if the condition is false, the execution will stop and further test steps will not be executed.
Verify: There won't be any halt in the test execution even though the verify condition is true or false.
How do you mock static methods?
That can be done using Power Mock.
What is ArgumentCaptor in Mockito?
Mockito ArgumentCaptor is utilized to capture arguments for mocked methods. ArgumentCaptor is utilized with Mockito verify() methods to get the arguments passed when any method is called. This way, we can provide supplemental JUnit assertions for our tests.
What is the difference between mocking and spying?
The difference is that in mock, we are engendering a complete mock or fake object while in spy, there is the real object and you just spying or stubbing concrete methods of it. While in spy objects, of course, since it is a genuine method when we are not stubbing the method, then it will call the real method behavior. If we optate to transmute and mock the method, then we require to stub it.
List some Mockito Annotations?
- @Mock - It is used to create and inject mocked instances.
- @Spy - It is used to create a real object and spy on the real object.
- @Captor - It is used to create an ArgumentCaptor.
- @InjectMocks - It is used to create an object of a class and insert its dependencies.
- @RunWith - It is utilized to keep the test clean and improves debugging. It additionally detects the unutilized stubs available in the test and initializes mocks annotated with @Mock annotation.
What is Hamcrest used for?
Hamcrest is a assert framework for testing libraries like JUnit. Hamcrest allows checking for conditions in our code via subsisting matchers classes. It additionally sanctions us to define our custom matcher implementations.