Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking vs. Spying in mocking frameworks

In mocking frameworks, you can mock an object or spy on it. What's the difference between the two and when would/should I use one over the other?

Looking at Mockito, for example, I see similar things being done using spies and mocks, but I am unsure as to the distinction between the two.

like image 277
Vivin Paliath Avatar asked Oct 16 '22 16:10

Vivin Paliath


People also ask

What is the difference between mock and spy?

Mocks are used to create fully mock or dummy objects. It is mainly used in large test suites. Spies are used for creating partial or half mock objects. Like mock, spies are also used in large test suites.

What is the difference between mocking and stubbing?

Stubbing, like mocking, means creating a stand-in, but a stub only mocks the behavior, but not the entire object. This is used when your implementation only interacts with a certain behavior of the object.

What is the difference between @mock and @MockBean?

We can use the @MockBean to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context. If no bean of the same type is defined, a new one will be added.

What is difference between @mock and @injectmock?

@InjectMocks creates an instance of the class and injects the mocks that are created with the @Mock annotations into this instance. @Mock is used to create mocks that are needed to support the testing of the class to be tested. @InjectMocks is used to create class instances that need to be tested in the test class.


2 Answers

Mock object replace mocked class entirely, returning recorded or default values. You can create mock out of "thin air". This is what is mostly used during unit testing.

When spying, you take an existing object and "replace" only some methods. This is useful when you have a huge class and only want to mock certain methods (partial mocking). Let me quote Mockito documentation:

You can create spies of real objects. When you use the spy then the real methods are called (unless a method was stubbed).

Real spies should be used carefully and occasionally, for example when dealing with legacy code.

When in doubt, use mocks.

like image 96
Tomasz Nurkiewicz Avatar answered Nov 27 '22 15:11

Tomasz Nurkiewicz


I'll try to explain using an example here:

// Difference between mocking, stubbing and spying
@Test
public void differenceBetweenMockingSpyingAndStubbing() {
    List list = new ArrayList();
    list.add("abc");
    assertEquals(1, list.size());

    List mockedList = spy(list);
    when(mockedList.size()).thenReturn(10);
    assertEquals(10, mockedList.size());
}

Here, we had initial real object list, in which we added one element and expected size to be one.

We spy real object meaning that we can instruct which method to be stubbed. So we declared that we stubbed method - size() on spy object which will return 10, no matter what is actual size.

In a nutshell, you will spy real object and stub some of the methods.

like image 28
user2775185 Avatar answered Nov 27 '22 15:11

user2775185