Mock vs Spy Both can be used to mock methods or fields. The difference is that in mock, you are creating a complete mock or fake object while in spy, there is the real object and you just spying or stubbing specific methods of it . Mock vs InjectMock @Mock creates a mock. @InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock (or @Spy ) annotations into this instance. Unit Testing Void Methods with Mockito and JUnit How to Test Void Methods As we already know that our aim is to test void methods in a class. But it is also really important to understand why we test void methods. Whenever we write unit test cases for any method, we expect a return value from the method. Generally, we use assert for checking if the method returns the value that we expect it to return, but in the case of void methods, they do not return any value. So how do we check if our method is functioning properly? Let’s...
Problem Statement In this problem, we are given a linked list with a loop. We have to find the first node of the loop in the linked list. Problem Statement Understanding Let’s try to understand the problem statement with the help of examples. Suppose the given list is: According to the problem statement, we need to find the starting node of the loop. From the linked list, we can see that there is a loop in the linked list starting at node with value 3 and containing 3 nodes 3, 5, and 7. The last node of the loop points back to the first node of the loop. As node with value 3 is the first node of the loop, so we will return 3 as output. Approach and Algorithm (Floyd’s Cycle Detection) Our approach will be simple: 1) Firstly, we have to detect the loop in the given linked list. For detecting a loop in any linked list, we know the most efficient algorithm is the Floyd Cycle detection Algorithm . 2) In Floyd's cycle detection algorithm, we initialize 2 pointers, slow ...
Comments
Post a Comment