Using Mocks for Testing in Your CI/CD Pipeline

Using Mocks for Testing in Your CI/CD Pipeline Bob Reselman (Guest Author) November 15, 2017

May 19, 2026 · 7 min read · CI/CD

Using Mocks for Testing in Your CI/CD Pipeline

Bob Reselman (Guest Author)
November 15, 2017

The Problems of Test Driven Development

A common problem that developer committed to Test Driven Development (TDD) have is quiz code that has dependencies which are under development or unavailable. This is where Mocks come in. For example, take a look at Figure 1 below. TheCustomerobject on the left side of the illustration depends upon use establish in theUseraim on the right.

Figure 1: Well design package encapsulates code allot to area of concerns

There is a trouble. Work is at a standstill. The developer of theUserobject can not complete their employment because they are waiting for the Data Team to finish working on the table and schema designs in the database. Thus, theCustomerdevelopers are waiting on theUserdevelopers and theUserdeveloper are waiting on the Data Team. Clearly the situation is unsufferable. But, what ’ s to be done? The answer is to use mocks.

A mock is a temporary aim or service that emulates behaviors that are expected to be delivered later on.

Using a mock object permit the developer to perform testing on his or her employment, while other development is underway. In the case illustrated in Figure 1, previously, the developer can prove the function,Customer.update (user_data), despite the fact that an extraneous habituation (s) might not be useable. The developer simply mocks the colony, in the case,User. Listing 1 below shows a developer defined mock,MockUser. As the name implies,MockUsermocks the expected behavior inUser.

module.exports = new MockUser;

 

function MockUser(){

& nbsp; & nbsp;this.validate = function validate (user_data) {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;return user_data?"OK" : "ERROR";

& nbsp; & nbsp;};

 this.get = function get (user_data) {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;if(user_data) {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; user_data.id = 1;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; user_data.isMock = true;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;return user_data;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;}

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;return "ERROR";

& nbsp; & nbsp;};

& nbsp; & nbsp;this.create = function create (user_data) {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; user_data.id = 1;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;return user_data;

& nbsp; & nbsp;};

& nbsp; & nbsp;this.update = function update (user_data) {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;return user_data?"OK" : "ERROR";

& nbsp; & nbsp;};

};

Listing 1:A unproblematic developer specify mock object in Node.JS

Listing 2 shows a way to implement mocking into an object that has external dependencies. In this case we ’ ll use aMockUserdirectly within theCustomerobject. The Node.JS code in Listing 2 chit to see if an surroundingsDEV_ENVexists and have be set to the value,test. If so, the mock is utilise.

'use strict ';

 

const mockUser = require(' .. /mocks/mockUser ');

const user = require('./user');

module.exports.update = function update (user_data) {

& nbsp; & nbsp;let u = user;

& nbsp; & nbsp;//check the growing surroundings to see if we 're in test

& nbsp; & nbsp;if(process.env.DEV_ENV & amp; & amp;process.env.DEV_ENV ==='test'){

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;//if we are, use the mock

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;u = mockUser;

& nbsp; & nbsp;}

& nbsp; & nbsp;let status = 'UNKNOWN'

& nbsp; & nbsp;if(u.validate(user_data) ==='ERROR') return 'ERROR';

& nbsp; & nbsp;// Check exploiter exists

& nbsp; & nbsp;const model = u.get(user_data);

& nbsp; & nbsp;// Model create/update code

& nbsp; & nbsp;if (model.id) {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;status = u.update(user_data);

& nbsp; & nbsp;}else {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;status = u.create(user_data);

& nbsp; & nbsp;}

& nbsp; & nbsp;return status

}

Listing 2:Using a mock in Customer.js

Once the mock is in place inCustomer.update (), the Customercode can now be tested as shown in Listing 3:

'use hard-and-fast ';

 

const chai = require('chai');

chai.should();

const assert = require('chai').assert;

const expect = require('chai').expect;

const describe = require('mocha').describe;

const it = require('mocha').it;

const customer = require(' .. /models/customer ');

describe('Basic Tests: ', () = & gt; {

& nbsp; & nbsp;it('Can update customer ', done = & gt; {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;const data = {};

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;data.id = 1;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;data.firstName = 'Bob';

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;data.lastName = 'Reselman ';

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;data.dob = new Date(' 7/29/1906 ');

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;assert.equal(customer.update(data), 'OK', ` Could not get status for${data}`);

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; done ();

& nbsp; & nbsp;});

& nbsp; & nbsp;it(' Can not update customer ', done = & gt; {

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;assert.equal(customer.update(), 'ERROR', ` Could not get status when legislate null data to the function `);

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; done ();

& nbsp; & nbsp;});

});

Listing 3:Mocking behavior in Customer allows the object to be essay

Mocking has been an important part of software testing for a long clip. Design model for using mocks are well known. In fact, a unscathed industriousness has evolved around developing and using mock. Most developers committed to unit testing use mocking fabric since mocking can get rather complex very apace. There is little sentiency in reinvent the wheel when a tried and true mock framework exists. Using a mock framework saves time and further developer to follow best practices. Atmabl, we use mocking services for two different portion of our application. & nbsp;Mockitoservice as the mocking tool we use when unit prove different part of our covering connect to back end data components. Mockito is designed for Java applications and is a democratic instrument among Java developers. In addition, given our front end expend Javascript with Node and a React framework, we useSinonfor mocking. Thislinkprovides a comprehensive list of mocking frameworks available. & nbsp; & nbsp;

Get the Code

You can detect the seed code that demonstrates how to make a developer defined mock on GitHub,here.

 

Mocking Services

In addition to using mocks to work with source code, you can also use mocks to emulate API behavior. For example, should your client side code have a colony that makesGETrequests to an API endpoint that is under maturation, you can use mock engineering to emulate the request/response behavior of the API endpoint. For instance, should you be evolve an API using Java, you can useWireMockto bemock your API. For .NET you can usemock4netand for Python there is,mock-server. The PHP community offershttp-mock.

One of the better techniques for implement mock services is to ensure that your evolution methodology supports theSpecification Firstattack to API design. Using Specification First mean that you project your API using one of the common API specifications,RAML, OpenAPI or API Blueprint. Then once the design is created employ a specification, you can use community tools to mechanically create simple mock servers against that specification. You can use a tool such asSwaggerHubto auto-generate a mock web waiter using a specification written OpenAPI. SwaggerHub permit you to generate the server in a variety of languages.go-raml-mockerallows you create mock web servers against a specification written in RAML. For those shops creating specification under API Blueprint, there isapi-blueprint-mockserver.

Putting It All Together

Mocking is a powerful technique that is utilise by many development shops dedicated to comprehensive testing in general, and Test Driven Development (TDD) in particular. Using mocks accelerate up development time while not sacrificing codification quality. It takes a bit of clip and attention to get comfortable employ mocks. But, once the conception is silent and mocking proficiency are mastered, developer, test engineer and testers will experience significant benefits. Mocking provides the powerfulness and flexibility to rapidly implement effective testing that is good suited for the Continuous Integration/Continuous Delivery pipeline. Adding mocks to your testing toolbox will amend both the value of the tryout you compose and the quality of the code you make.

Quality Engineering Resources

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free