Reputation: 145
I have an API on Anypoint Platform, it's important specs are:
I built the Mule flows using the RAML v1.0 having the above specs, and all functionality works fine. Now, I am trying to develop the mUnit for the mule flows. For a test case I set the below event and call flowReference to Mule flow.
#[{
headers: {
'client_space': 'ABCD-1234',
'client_code': 'XYZ-0001'
},
'queryParams': {
'key': '12345678',
'isAllow': true
},
'requestPath': '/api/retrieveInformation',
'rawRequestPath': '/api/retrieveInformation',
'requestUri': '',
'listenerPath': '/api/*',
'relativePath': '/retrieveInformation',
'method': 'GET',
'scheme': 'HTTP',
'version': 'HTTP/1.1',
'localAddress': '/127.0.0.1:8091',
'remoteAddress': ''
} as Object {
class: 'org.mule.extension.http.api.HttpRequestAttributes'
}]
Now running the units, APIKit:Router throws error i.e., cannot cast case from Boolean to String. If I change to " 'isAllow': 'true' ", then APIKit:Router throws valid error APIKIT:NOT_IMPLEMENTED.
Can anyone help how to achieve this scenario without changing API Specs?
Upvotes: 0
Views: 1402
Reputation: 25699
Internal classes from the connector (class: 'org.mule.extension.http.api.HttpRequestAttributes'
) are not meant to be used directly in tests. This is not a good idea to implement tests. It is better to try to resolve what error you had originally with the test creation. Be sure to be using the latest version of Studio.
There may be a problem with the API. If it still fails you can create a test suite for a simpler API and then use it as an example on how to build your test cases manually using the HTTP Requester instead of trying to mock the requests. Example:
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd http://www.mulesoft.org/schema/mule/munit-tools http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd ">
<munit:config name="simple-api-spec-apikit-test.xml" />
<http:request-config name="HTTP_Request_Configuration" basePath="/api">
<http:request-connection host="localhost" port="8081" />
</http:request-config>
<munit:test name="get:\ping:simple-api-spec-config-200-application\json-FlowTest" description="Verifying functionality of [get:\ping:simple-api-spec-config-200-application\json]">
<munit:enable-flow-sources>
<munit:enable-flow-source value="simple-api-spec-main" />
<munit:enable-flow-source value="get:\ping:simple-api-spec-config" />
</munit:enable-flow-sources>
<munit:execution>
<http:request config-ref="HTTP_Request_Configuration" method="GET" path="/ping">
<http:headers>#[{"Accept":"application/json"}]</http:headers>
</http:request>
</munit:execution>
<munit:validation>
<munit-tools:assert-that expression="#[attributes.statusCode]" is="#[MunitTools::equalTo(200)]" message="The HTTP Status code is not correct!" doc:name="Assert That Status Code is 200" />
<munit-tools:assert-that expression="#[output application/java ---write(payload, 'application/json') as String]" is="#[MunitTools::equalTo(MunitTools::getResourceAsString('scaffolder/response/get_200_ping_application_json.json'))]" message="The response payload is not correct!" doc:name="Assert That - Payload is Expected" />
</munit:validation>
</munit:test>
</mule>
Upvotes: 1