简体   繁体   中英

How to write negative test cases in Munit

I am writing a negative test case where in I have to create an exception in flow. I want Global exception strategy to catch it, so that I can assert the response. Inside GlobalExceptionStrategy, we are setting some error code based on the exception error code. Intention is to make sure that the Exception strategy is working.

Issue is as follows.. I can run the flow using runFlow(). this method will throw MuleException and it will never invoke the exception starategy.

Is there any way I can feed this to exception strategy an then get the reponse from it...?

There are a couple of things that I don't follow in your example so I'll try do a best effort making assumptions and we can take it from there.

First thing, you shouldn't validate that the exception strategy is working properly. That is, if you trow an exception you need to assume the exception strategy (which is a Mule component) is working as it should and it'll catch the exception. Otherwise you'll end up testing each component.

A more accurate test would be to ensure that the flow is throwing the proper exception (which you can do with MUnit).

If what you want to validate is that the logic inside the exception strategy is working properly, I would say it's complex enough for you to want to test it, thus it should be at least in a sub-flow. In that way you can validate the sub-flow is working properly.

Now more to the point of what you described, if you do a runFlow and the exception is not getting cached is because the production code may a have an error. You mention a global exception strategy, so based on that here is a example code on (what I think) you code should look like:

 <configuration defaultExceptionStrategy-ref="zCatch_Exception_Strategy" doc:name="Configuration" />
    <flow name="zFlow">
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy"><![CDATA[throw new java.lang.RuntimeException()]]></scripting:script>
        </scripting:component>

    </flow>
    <catch-exception-strategy name="zCatch_Exception_Strategy">
        <logger message="+++ ERROR" level="INFO" doc:name="Logger"/>
        <set-variable variableName="error_var" value="#['value]" doc:name="Variable"/>
    </catch-exception-strategy>

Please notice that in addition to the global exception strategy you also need to define a defaultExceptionStrategy

<configuration defaultExceptionStrategy-ref="zCatch_Exception_Strategy" doc:name="Configuration" />

This will cause your flow exceptions to be handle by that one. If you do not do that the exception strategy will never know an exception has been thrown.

That said here is the test code:

  <munit:test name="z-test-suite-zFlowTest" description="Test">
        <flow-ref name="zFlow" doc:name="Flow-ref to zFlow"/>
        <munit:assert-on-equals message="oops" expectedValue="#['value']" actualValue="#[flowVars['error_var']]" doc:name="Assert Equals"/>
    </munit:test>

Now as you said you used runFlow, I reckon you are working with Java based test (although I would advise you to use the XML based test approach), here is a Java example:

@Test
    public void theTest() throws MuleException, Exception {
        MuleEvent result = runFlow("zFlow", testEvent(""));

        String v = (String) result.getMessage().getProperty("error_var", PropertyScope.INVOCATION);
        Assert.assertEquals("value", v);
    }

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM