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. The<span>Customer</span>object on the left side of the illustration depends upon use establish in the<span>User</span>aim 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 the<span>User</span>object 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, the<span>Customer</span>developers are waiting on the<span>User</span>developers and the<span>User</span>developer 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,<span>Customer.update (user_data)</span>, despite the fact that an extraneous habituation (s) might not be useable. The developer simply mocks the colony, in the case,<span>User</span>. Listing 1 below shows a developer defined mock,<span>MockUser</span>. As the name implies,<span>MockUser</span>mocks the expected behavior in<span>User</span>.

<strong>module</strong><strong>.</strong><strong>exports </strong><strong>= new </strong><strong><i>MockUser</i></strong><strong>;</strong>

 

<strong>function </strong><i><span>MockUser</span></i><span>(){</span>

<span>& nbsp; & nbsp;</span><strong>this</strong><span>.</span><span>validate </span><span>= </span><strong>function </strong><span>validate (user_data) {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>return </strong><span>user_data?</span><strong>"OK" </strong><span>: </span><strong>"ERROR"</strong><span>;</span>

<span>& nbsp; & nbsp;};</span>

<span>  </span><strong>this</strong><span>.</span><span>get </span><span>= </span><strong>function </strong><span>get (user_data) {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>if</strong><span>(user_data) {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; user_data.</span><strong>id </strong><span>= </span><span>1</span><span>;</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; user_data.</span><strong>isMock </strong><span>= </span><strong>true</strong><span>;</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>return </strong><span>user_data;</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;}</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>return </strong><strong>"ERROR"</strong><span>;</span>

<span>& nbsp; & nbsp;};</span>

<span>& nbsp; & nbsp;</span><strong>this</strong><span>.</span><span>create </span><span>= </span><strong>function </strong><span>create (user_data) {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; user_data.</span><strong>id </strong><span>= </span><span>1</span><span>;</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>return </strong><span>user_data;</span>

<span>& nbsp; & nbsp;};</span>

<span>& nbsp; & nbsp;</span><strong>this</strong><span>.</span><span>update </span><span>= </span><strong>function </strong><span>update (user_data) {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>return </strong><span>user_data?</span><strong>"OK" </strong><span>: </span><strong>"ERROR"</strong><span>;</span>

<span>& nbsp; & nbsp;};</span>

<span>};</span>

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 a<span>MockUser</span>directly within the<span>Customer</span>object. The Node.JS code in Listing 2 chit to see if an surroundings<span>DEV_ENV</span>exists and have be set to the value,<span>test</span>. If so, the mock is utilise.

<strong>'use strict '</strong><span>;</span>

 

<strong>const </strong><strong>mockUser </strong><span>= </span><i><span>require</span></i><span>(</span><strong>' .. /mocks/mockUser '</strong><span>);</span>

<strong>const </strong><strong>user </strong><span>= </span><i><span>require</span></i><span>(</span><strong>'./user'</strong><span>);</span>

<span>module</span><span>.</span><strong>exports</strong><span>.</span><span>update </span><span>= </span><strong>function </strong><span>update (user_data) {</span>

<span>& nbsp; & nbsp;</span><strong>let </strong><span>u </span><span>= </span><strong>user</strong><span>;</span>

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

<i><span>& nbsp; & nbsp;</span></i><strong>if</strong><span>(</span><span>process</span><span>.</span><strong>env</strong><span>.DEV_ENV & amp; & amp;</span><span>process</span><span>.</span><strong>env</strong><span>.DEV_ENV ===</span><strong>'test'</strong><span>){</span>

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

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><i><span>//if we are, use the mock</span></i>

<i><span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span></i><span>u </span><span>= </span><strong>mockUser</strong><span>;</span>

<span>& nbsp; & nbsp;}</span>

<span>& nbsp; & nbsp;</span><strong>let </strong><span>status </span><span>= </span><strong>'UNKNOWN'</strong>

<strong>& nbsp; & nbsp;</strong><strong>if</strong><span>(</span><span>u</span><span>.</span><span>validate</span><span>(user_data) ===</span><strong>'ERROR'</strong><span>) </span><strong>return </strong><strong>'ERROR'</strong><span>;</span>

<span>& nbsp; & nbsp;</span><i><span>// Check exploiter exists</span></i>

<i><span>& nbsp; & nbsp;</span></i><strong>const </strong><span>model </span><span>= </span><span>u</span><span>.</span><span>get</span><span>(user_data);</span>

<span>& nbsp; & nbsp;</span><i><span>// Model create/update code</span></i>

<i><span>& nbsp; & nbsp;</span></i><strong>if </strong><span>(</span><span>model</span><span>.</span><strong>id</strong><span>) {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><span>status </span><span>= </span><span>u</span><span>.</span><span>update</span><span>(user_data);</span>

<span>& nbsp; & nbsp;}</span><strong>else </strong><span>{</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><span>status </span><span>= </span><span>u</span><span>.</span><span>create</span><span>(user_data);</span>

<span>& nbsp; & nbsp;}</span>

<span>& nbsp; & nbsp;</span><strong>return </strong><span>status</span>

<span>}</span>

Listing 2:Using a mock in Customer.js

Once the mock is in place in<span>Customer.update ()</span>, the <span>Customer</span>code can now be tested as shown in Listing 3:

<strong>'use hard-and-fast '</strong><span>;</span>

 

<strong>const </strong><span>chai </span><span>= </span><i><span>require</span></i><span>(</span><strong>'chai'</strong><span>);</span>

<span>chai</span><span>.</span><strong>should</strong><span>();</span>

<strong>const </strong><span>assert </span><span>= </span><i><span>require</span></i><span>(</span><strong>'chai'</strong><span>).assert;</span>

<strong>const </strong><span>expect </span><span>= </span><i><span>require</span></i><span>(</span><strong>'chai'</strong><span>).expect;</span>

<strong>const </strong><span>describe </span><span>= </span><i><span>require</span></i><span>(</span><strong>'mocha'</strong><span>).</span><strong>describe</strong><span>;</span>

<strong>const </strong><span>it </span><span>= </span><i><span>require</span></i><span>(</span><strong>'mocha'</strong><span>).</span><strong>it</strong><span>;</span>

<strong>const </strong><span>customer </span><span>= </span><i><span>require</span></i><span>(</span><strong>' .. /models/customer '</strong><span>);</span>

<span>describe</span><span>(</span><strong>'Basic Tests: '</strong><span>, () = & gt; {</span>

<span>& nbsp; & nbsp;</span><span>it</span><span>(</span><strong>'Can update customer '</strong><span>, done = & gt; {</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><strong>const </strong><span>data </span><span>= {};</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><span>data</span><span>.</span><strong>id </strong><span>= </span><span>1</span><span>;</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><span>data</span><span>.</span><strong>firstName </strong><span>= </span><strong>'Bob'</strong><span>;</span>

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;</span><span>data</span><span>.</span><strong>lastName </strong><span>= </span><strong>'Reselman '</strong><span>;</span>

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

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

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; done ();</span>

<span>& nbsp; & nbsp;});</span>

<span>& nbsp; & nbsp;</span><span>it</span><span>(</span><strong>' Can not update customer '</strong><span>, done = & gt; {</span>

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

<span>& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; done ();</span>

<span>& nbsp; & nbsp;});</span>

<span>});</span>

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