30 Days of API Testing Day 14: Compare and contrast mocking, stubbing, and faking

I found an article explain quite details about Mock, Stub and Fake:

Mock: Mocks are objects that register calls they receive.
In test assertion we can verify on Mocks that all expected actions were performed.

Stub: Stub is an object that holds predefined data and uses it to answer calls during tests. It is used when we cannot or don’t want to involve objects that would answer with real data or have undesirable side effects.

Fake: Fakes are objects that have working implementations, but not same as production one. Usually they take some shortcut and have simplified version of production code.

6 Likes

As to my experience, I’ve used mocks only couple of times.
From the definitions above (thanks to ocennguyen) I understood the following:

  • when I want to test if the particular API call reaches server (event) I need to use mock,
  • when I want to test how API service will resolve server response with particular (set up before test) data or parameters I need to use stub,
  • fake is a functionality that similar to production one, but has restrictions (e.g. sandbox mode for any API).

Also I want to share the discussion about mocks, stubs, fakes https://stackoverflow.com/questions/346372/whats-the-difference-between-faking-mocking-and-stubbing

5 Likes

There are loads of different terms out there (a lot of it is derived from unit testing techniques too, so you will need to get your analytical hats on to apply them at different levels), you will hear them used interchangeably, like lots of terms in the software development world. I use the Martin Fowler definitions mainly. He seems trustworthy. :grinning:

Term Description
Fakes A fake usually has the same behaviour as the thing it will replace, perhaps in a more lightweight fashion. Without a persistent database for example.
Stubs A stub will provide a set response to that needed by the test, usually it will not respond to anything outside the test.
Spies Spies have the behaviour of a stub, with the additional ability to record meta information about how they were requested, and/or how many times it was requested.
Mocks Mocks form a specification of what requests they expect to receive and what format they take. If the request and format do not meet the contract expected by the mock, the tests will fail.
Doubles This is a catch all for the terms we will use above.

Terminology you might hear or have heard in the world:

Term Description
Dummies These are usually objects that may be passed around in tests but never actually used. Often, they just fill up lists of parameters that a method needs to be invoked.
Shims or Moles Shims are generally used to provide objects from assemblies outside of your solution, such as external operating system dll’s in the .NET world

A couple of references, as I can’t post more than 2 links:

5 Likes

I found an article explain quite details about Mock, Stub and Fake

Mock : A class that implements an interface and allows the ability to dynamically set the values to return/exceptions to throw from particular methods and provides the ability to check if particular methods have been called/not called.

Stub : Like a mock class, except that it doesn’t provide the ability to verify that methods have been called/not called.

Fake : Fakes are objects that implements an interface but contains fixed data and no logic. Simply returns “good” or “bad” data depending on the implementation.

4 Likes

@conorfi has a lengthy Twitter thread about this :slight_smile:

2 Likes

What is the Use of Mock, Stub, Fake in API Testing?
Where do we use this?

Hello @srinivasskc!

The primary use of mocks, stubs, or fakes is to isolate parts of the API for testing. Isolation provides for smaller, quicker tests, and can help pinpoint problems.

For example, say the API validates inputs before updating a database, and I want to evaluate those validations. I construct a mock for the updates to the database and create tests to exercise the validations. Since the database is mocked (that is, the mock acts like an update to the database for the API but actually does nothing), I can isolate the evaluation to just the validations. Does that help?

Joe

3 Likes

3 Likes

I found some references as:

And I summarized as below:

Mock-Stub-Fake

4 Likes

I also found the useful reference of mocking, stubbing and fucking on stackoverflow.

  • Stub - an object that provides predefined answers to method calls.
  • Mock - an object on which you set expectations.
  • Fake - an object with limited capabilities (for the purposes of testing), e.g. a fake web service.
3 Likes

I think there is no good consensus on the definition of the terms. Reading the above definitions there appear to be some conflicts, or at least ambiguities in, the definitions people are finding.

I’m not sure of the value of trying to define exactly what each one should mean. If I want to write a test and return fixed data from a given call I don’t care what its called. If I want to return different data given certain inputs in a way that mimics some of the real code base I’m testing then I’ll do it - but I don’t care what its called. If I want to assert that a call has occurred I’ll do it - possibly by replacing the function to be called, but possibly through execution tracing - I don’t care that the specific replacement has a categorisation.

Maybe I’m missing the point, but the overall technique of replacing part of the software / system under test (be that internal, external etc.) with something is important. What you call a subtle difference with what you specifically do isn’t - but this is only my opinion.

1 Like

I found the comparison below:

Fake : a class that implements an interface but contains fixed data and no logic. Simply returns “good” or “bad” data depending on the implementation.

Mock : a class that implements an interface and allows the ability to dynamically set the values to return/exceptions to throw from particular methods and provides the ability to check if particular methods have been called/not called.

Stub : Like a mock class, except that it doesn’t provide the ability to verify that methods have been called/not called.

Thanks @kmstuanhnguyen for the article from stack-overflow:

3 Likes

I am reading below concepts about mock, stub and fake:

  • A Mock Object is a powerful way to implement Behavior Verification while avoiding Test Code Duplication between similar tests by delegating the job of verifying the indirect outputs of the system under test entirely to a Test Double.

  • In many circumstances, the environment or context in which the system under test operates very much influences the behavior of the system under test. To get good enough control over the indirect inputs of the SUT, we may have to replace some of the contexts with something we can control, a Test Stub.

  • The system under test often depends on other components or systems. The interactions with these other components may be necessary but the side-effects of these interactions as implemented by the real depended-on component, may be unnecessary or even detrimental.
    A Fake Object is a much simpler and lighter weight implementation of the functionality provided by the depended-on component without the side effects we choose to do without.

3 Likes

1 Like