Ion Utale
Ion Utale

Reputation: 697

MuleSoft - how to catch an error/exception into Munit test?

Into a flow i raised and error and i would like to test it in Munit.

The documentation doesn't seem to contain and explain this particular case.

what is the text that i need to insert into :

enter image description here

Upvotes: 1

Views: 7733

Answers (1)

Ryan Carter
Ryan Carter

Reputation: 11606

All errors thrown in Mule contain meta-data including a TYPE.

For example, here is a list of some specific HTTP: errors thrown by the HTTP module:

  • HTTP:UNSUPPORTED_MEDIA_TYPE

    HTTP:CONNECTIVITY

    HTTP:INTERNAL_SERVER_ERROR

    HTTP:METHOD_NOT_ALLOWED

Each module's documentation should contain all specific error types thrown by that module. Here is the HTTP one example: https://docs.mulesoft.com/connectors/http/http-documentation#throws

In your screenshot for example it uses APIKIT module. APIKIT module has its own errors again. Think of it as certain Java classes throwing custom exceptions specific to that class.

And here is a full list of core error types you can catch like EXPRESSION for example:

https://docs.mulesoft.com/mule-runtime/4.1/mule-error-concept

The attribute expectedErrorType expects an error type ID that needs to be defined inside the application being tested. This attribute allows you to validate that a defined error type in your application is thrown. If you define an errorType that does not exists in your application, the test does not run.

<munit:test name="MUnit-test-suite"
  description="Test Error Type"
  expectedErrorType="FTP:ILLEGAL_PATH">
  ...
</munit:test>

This Error Type test expects that an FTP operation will throw an FTP:ILLEGAL_PATH error.

You will need to configure this for your specific type you are expecting.

The attribute expectException expects a literal exception class name (canonical form). When you provide a literal value, it should take the form of the canonical class name of the exception that is expected. In these cases, Mule always throws a MuleMessagingException. MUnit validates the provided classname if the underlying cause of the MuleMessagingException thrown is of the exact same type.

<munit:test name="testExceptions"
  description="Test Exceptions"
  expectedException="java.lang.RuntimeException">
  ...
</munit:test>

If you define that your test expects an exception and none is thrown, the test fails immediately.

So you don't need to provide both.

Upvotes: 2

Related Questions