Search code examples
mulemule-studiomulesoftmunit

MUNIT4 :java.util.LinkedHashMap cannot be cast to org.mule.extension.http.api.HttpRequestAttributes


Imtrying munit set-event processor;

<munit:behavior >
            <munit:set-event doc:name="Set Event" doc:id="3ffb4e58-3938-4316-9533-f0031ad492a6" >
                <munit:payload value='{ .. }' encoding="UTF-8" mediaType="application/json" />
                <munit:attributes value="#[{headers:{'application-id':'test'},method:'POST'}]" />

            </munit:set-event>

It reads my payload and attributes coreectly. I put a log and checked. But after that I get;

ERROR 2020-05-21 15:46:23,622 [[MuleRuntime].cpuLight.05: [xxxx-api-v1].raml-main.CPU_LITE @41d3521e] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
********************************************************************************
Message               : java.util.LinkedHashMap cannot be cast to org.mule.extension.http.api.HttpRequestAttributes.
Element               : raml-main/processors/4 @ xxxxxx-api-v1:api.xml:57
Element XML           : <apikit:router config-ref="raml-config"></apikit:router>
Error type            : MULE:UNKNOWN
--------------------------------------------------------------------------------
Root Exception stack trace:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to org.mule.extension.http.api.HttpRequestAttributes
    at org.mule.module.apikit.Router.processEvent(Router.java:133)
    at org.mule.module.apikit.Router.processWithExtension(Router.java:122)
    at org.mule.runtime.core.privileged.processor.MessageProcessors.lambda$flatMap$13(MessageProcessors.java:338)

Why is that? what is wrong in my set-event processor?


Solution

  • It looks like the MUnit test is trying to mock the HTTP Listener, by setting the attributes of the HTTP request received in an expression. That expression translates into a Java Map. But APIKit expects the actual class that implements the HTTP Listener attributes. Mocking in this way will not work.

    To implement it the right way, in Anypoint Studio right click on the APIKit router and click in the menu entry Create Test Suite for XXX.xml from API specification. That will create a skeleton for your tests.

    Alternatively you can check this example in Anypoint Exchange: https://anypoint.mulesoft.com/exchange/org.mule.examples/testing-apikit-with-munit/