Mock HTTP Responses (mockserver plugin)

Mock HTTP Responses (mockserver plugin)

When testing API, sometimes it becomes necessary to have a mocking server which imitates the real API server and provides a realistic mock response. The mocking matches the API request and responses for mapped APIs which resides under mappings directory


Dependendcy
<dependency>
    <groupId>io.testifi.cast</groupId>
    <artifactId>cast-http-mock</artifactId>
    <scope>test</scope>
</dependency>


The mocking works using client server architecture.


1. Mock Server

Usage

${@mockserver(port!, mappingDirectory!)} 


! -mandatory , ? - optional

Mock server must be set in a <put-up> and then included inside a <suite>

Examples

Example

Behaviour

Server Setup (file can be placed under resources/objects)

mock-server.tto
<include id="mock-server">
    <put-up id="mockServer">
    	<property name="mockPort" value="${mockPort!'9009'}"/>
        <property name="mockServerUrl" value="localhost:${mockPort}"/>
    	
     	<property name="mockServer" value="${@mockserver(mockPort, 'src/test/resources/')}" /> 
       
	    <tear-down>
	      <script>
	            ${mockServer.stop()}
	        </script>
	    </tear-down> 
	</put-up>
</include>

Server inclusion in Suite

<suite id="cast-http-mock:debug">

	<mock-server description="spin up the local http mock server"/>
	
</suite>

The mock server should be set in a <put-up> and then it will be used by all scenarios inside a suite. The mock server is called inside a suite.


2. Mock Client

Sample Mock Mapping

The suggested location to place the mock mappings is under src/test/resources/payload/mocks

something.json
{
    "request": {
        "method": "GET",
        "url": "/some/thing"
    },
    "response": {
        "status": 200,
        "body": "Mocked Response!",
        "headers": {
            "Content-Type": "text/plain"
        }
    }
}

Usage

${@createMapping(apiMappingFile!)}

! -mandatory , ? - optional

Examples

Example

Behaviour

<scenario id="createMappings"
		xray:testId=""
        xray:testSummary="Create mock mapping scenario"
        xray:testDescription="Positive Scenario: create a new mapping for api mocking">
	
		
		<test-step xray:stepDescription="GIVEN a valid mockserver is running WHEN I create a new api mock mapping" xray:stepData="" xray:stepExpectedResult="Response Status: 200">
			<property name="mockApiMapping" value="${@createMapping(@file('/payloads/mocks/something.json', 'content'))}"/>
		</test-step>
		
		<test-step xray:stepDescription="AND I make an http call to mocked api" xray:stepData="" xray:stepExpectedResult="Response Status: 200">
			<http:call method="get" url="${mockServerUrl}"/>
		</test-step>
		
		<test-step xray:stepDescription="THEN i verify the mapping is created and mocked response is served" xray:stepData="" xray:stepExpectedResult="Response Status: 200">
			<assert method="contains" actualValue="${mockApiMapping}" comparedValue="Mocked Response!"/>
		</test-step>
		
</scenario>

Mocked api mapping creation and then calling the mocked api

Examples

Using mock server we can imitate the faulty behaviours of APIs out-of-the-box using following endpoints, once mock server is up and running.



Related content