Home > Back-end >  WSO2 unit testing Mock Service is not working
WSO2 unit testing Mock Service is not working

Time:09-16

I`m using the 2SO2 integration studio version is 8.0.0. I tried to write junit suite test for my app using mock-service for my endpoint. But when I tried to run the test I got the next failed :

[INFO]  ---------------------- ---------------- --------------- --------------- 
    [INFO] |   TEST CASE          |   DEPLOYMENT   |   MEDIATION   |   ASSERTION   |
    [INFO]  ====================== ================ =============== =============== 
    [INFO] | Test Case - TestMock |    PASSED      |    PASSED     |    FAILED     |
    [INFO]  ---------------------- ---------------- --------------- --------------- 
    [INFO] 
    [INFO] Failed Test Case(s): 
    [INFO]  ---------------------- ------------------- -------------------------------------- 
    [INFO] |   TEST CASE          |   FAILURE STATE   |       EXCEPTION / ERROR MESSAGE      |
    [INFO]  ====================== =================== ====================================== 
    [INFO] | Test Case - TestMock |    ASSERTION      | not equals                           |
    [INFO]  ---------------------- ------------------- -------------------------------------- 
    [INFO] 
    [INFO] Failed Assertion(s): 
    [INFO]  ---------------------- ----------------------- ------------------------------------------------------------------------------------------------------ 
    [INFO] |   TEST CASE          |   ASSERT EXPRESSION   |       FAILURE                                                                                        |
    [INFO]  ====================== ======================= ====================================================================================================== 
    [INFO] | Test Case - TestMock | assertEquals - $body  | Actual Response:                                                                                     |
    [INFO] |                      |                       | null                                                                                                 |
    [INFO] |                      |                       |                                                                                                      |
    [INFO] |                      |                       | Expected Response:                                                                                   |
    [INFO] |                      |                       | <jsonObject><r030>840</r030><txt>Долар США</txt><rate>36.5686</rate><cc>USD</cc><exchangedate> |
    [INFO] |                      |                       | 15.09.2022</exchangedate></jsonObject>                                                               |
    [INFO] |                      |                       |                                                                                                      |
    [INFO] |                      |                       | Description:                                                                                         |
    [INFO] |                      |                       | Tested service url - POST: http://localhost:8290/lma/currency                                        |
    [INFO] |                      |                       | Received status code - 202                                                                           |
    [INFO]  ---------------------- ----------------------- ------------------------------------------------------------------------------------------------------ 
    [INFO] 
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  15.103 s
    [INFO] Finished at: 2022-09-15T17:09:16 03:00

The test doesn't seem to start a mock-service. I can not undestand why ? It`s seems loke the studio ignore it.

My mock-service is set up with port = 9090 ( i do not know it`s right or not, this port i found in documentation by wso2 : https://ei.docs.wso2.com/en/7.2.0/micro-integrator/develop/creating-unit-test-suite/):

<mock-service>
    <service-name>Exchange</service-name>
    <port>9090</port>
    <context>/lma</context>
    <resources>
        <resource>
            <sub-context>/currency</sub-context>
            <method>POST</method>
            <request>
                <payload><![CDATA[{"currency": "USD"}]]></payload>
                <headers>
                    <header name="Content-Type" value="application/json"/>
                </headers>
            </request>
            <response>
                <status-code>200</status-code>
                <payload><![CDATA[<jsonObject><r030>840</r030><txt>Долар США</txt><rate>36.5686</rate><cc>USD</cc><exchangedate>]]></payload>
                <headers>
                    <header name="Content-Type" value="application/json"/>
                </headers>
            </response>
        </resource>
    </resources>
</mock-service>

Test is :

<unit-test>
    <artifacts>
        <test-artifact>
            <artifact>/LmaAPIConfigs/src/main/synapse-config/api/LmaAPI.xml</artifact>
        </test-artifact>
        <supportive-artifacts/>
        <registry-resources/>
        <connector-resources/>
    </artifacts>
    <test-cases>
        <test-case name="TestMock">
            <input>
                <request-path>/currency</request-path>
                <request-method>POST</request-method>
                <request-protocol>http</request-protocol>
                <payload><![CDATA[{"currency": "USD"}]]></payload>
                <properties>
                    <property name="Content-Type" scope="transport" value="application/json"/>
                </properties>
            </input>
            <assertions>
                <assertEquals>
                    <actual>$body</actual>
                    <expected><![CDATA[<jsonObject><r030>840</r030><txt>Долар США</txt><rate>36.5686</rate><cc>USD</cc><exchangedate>]]></expected>
                    <message>not equals</message>
                </assertEquals>
                <assertNotNull>
                    <actual>$body</actual>
                    <message>body is null</message>
                </assertNotNull>
            </assertions>
        </test-case>
    </test-cases>
    <mock-services>
        <mock-service>/LmaAPI/LmaAPIConfigs/test/resources/mock-services/Exchange.xml</mock-service>
    </mock-services>
</unit-test>

CodePudding user response:

Mock Service port is simply a port you want your Mock service to start on, this can be any arbitrary port that is not occupied by any other service. So in your case, if any other service is not using the port 9090 you can use this. As you can see here in the code, a new Emulator will be started with this port and the context you are providing to facilitate mocking.

When you create a Mock service, you will be mocking an Endpoint. So I assume you already have an Endpoint Defined, and trying to mock this. If that's the case you need to add that Endpoint to the <supportive-artifacts/> section, in your Test Suite. Something like the below.

<supportive-artifacts>
            <artifact>PATH_TO_ENDPOINT</artifact>
</supportive-artifacts>

I'm not exactly sure why you are getting a Received status code - 202 as the response. But it typically means your integration is unable to run.(Probably due to the missing endpoint). Also, it's important to note that all the detailed logs will be logged on the server side. So you won't be able to figure out what's happening by just looking at the Maven log. For example, as you can see here the server should log a message when your mock service is started. So make sure you check the server-side logs to identify any issues. If you are just executing from Integration Studio, the logs are located at <INTEGRATION_STUDIO_HOME>/runtime/microesb/repository/logs/wso2carbon.log

CodePudding user response:

Integration Studio 8.0.0 was having some bugs related to the unit testing and mock services. AFAIK the issue you observed (Receiving a 202 status code) and the one @ycr have mentioned (Missing endpoint config in the <supportive-artifacts/> [1]) have been fixed in the latest Integration Studio 8.1.0.

Can you try this in the latest updated Integration Studio 8.1.0 pack? You can download the latest version from the official website. Please refer Get the latest updates to install the latest updates to Integration Studio.

  • Related