How to use Mock component in
MUnit
04-04-2017
Abstract
• The Mock feature provided by MUnit allows
you to define mocked behavior for a message
processor. In this case, MUnit replaces the
normal behavior of the message processor
with the behavior you define. Thus you can
modify how a specific message processor
responds when it is called with a particular set
of attributes.
• .mflow
• <?xml version="1.0" encoding="UTF-8"?>
• <mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
• xmlns:spring="http://www.springframework.org/schema/beans"
• xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
• xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-current.xsd
• 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:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8089"
doc:name="HTTP Listener Configuration"/>
• <flow name="MockFlow">
• <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
• <set-payload value="Hi" doc:name="Set Payload"/>
• <logger message="------#[payload]----" level="INFO" doc:name="Logger"/>
• </flow>
• </mule>
• <?xml version="1.0" encoding="UTF-8"?>
• <mule xmlns:mock="http://www.mulesoft.org/schema/mule/mock" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:munit="http://www.mulesoft.org/schema/mule/munit"
xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit
http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd
• http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
• http://www.mulesoft.org/schema/mule/mock http://www.mulesoft.org/schema/mule/mock/current/mule-mock.xsd">
• <munit:config name="munit" doc:name="MUnit configuration"/>
• <spring:beans>
• <spring:import resource="classpath:Mock.xml"/>
• </spring:beans>
• <munit:test name="Mock-test-suite-MockFlowTest" description="Test">
• <mock:when messageProcessor=".*:.*" doc:name="Mock">
• <mock:with-attributes>
• <mock:with-attribute name="doc:name" whereValue="#['Set Payload']"/>
• </mock:with-attributes>
• <mock:then-return payload="Good Morning!"/>
• </mock:when>
• <flow-ref name="MockFlow" doc:name="MockFlow"/>
• <munit:assert-on-equals expectedValue="Good Morning!" actualValue="#[payload]" doc:name="Assert Equals"/>
• </munit:test>
• </mule>
• Output:
• Reference:
https://docs.mulesoft.com/munit/v/1.1.1/mock
-message-processor

Mock component in munit

  • 1.
    How to useMock component in MUnit 04-04-2017
  • 2.
    Abstract • The Mockfeature provided by MUnit allows you to define mocked behavior for a message processor. In this case, MUnit replaces the normal behavior of the message processor with the behavior you define. Thus you can modify how a specific message processor responds when it is called with a particular set of attributes.
  • 4.
    • .mflow • <?xmlversion="1.0" encoding="UTF-8"?> • <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" • xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" • xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd • 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:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8089" doc:name="HTTP Listener Configuration"/> • <flow name="MockFlow"> • <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/> • <set-payload value="Hi" doc:name="Set Payload"/> • <logger message="------#[payload]----" level="INFO" doc:name="Logger"/> • </flow> • </mule>
  • 6.
    • <?xml version="1.0"encoding="UTF-8"?> • <mule xmlns:mock="http://www.mulesoft.org/schema/mule/mock" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:munit="http://www.mulesoft.org/schema/mule/munit" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd • http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd • http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd • http://www.mulesoft.org/schema/mule/mock http://www.mulesoft.org/schema/mule/mock/current/mule-mock.xsd"> • <munit:config name="munit" doc:name="MUnit configuration"/> • <spring:beans> • <spring:import resource="classpath:Mock.xml"/> • </spring:beans> • <munit:test name="Mock-test-suite-MockFlowTest" description="Test"> • <mock:when messageProcessor=".*:.*" doc:name="Mock"> • <mock:with-attributes> • <mock:with-attribute name="doc:name" whereValue="#['Set Payload']"/> • </mock:with-attributes> • <mock:then-return payload="Good Morning!"/> • </mock:when> • <flow-ref name="MockFlow" doc:name="MockFlow"/> • <munit:assert-on-equals expectedValue="Good Morning!" actualValue="#[payload]" doc:name="Assert Equals"/> • </munit:test> • </mule>
  • 7.
  • 8.