Search code examples
filecsvmuleendpointmunit

MUnit test fails - Cannot process event as "getCSVAccountsFlow" is stopped MULE_ERROR-166


I created a flow that has an input of a csv file, then uses DataWeave to transform to JSON and then loops through each records and logs the payload - simple, works fine.

I then created the following MUnit using the getResources method of the Mock component; however, when I run the MUnit test, I get the following error:

ERROR:

ERROR 2015-12-06 15:25:48,613 [main] 
org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Cannot process event as "getCSVAccountsFlow" is 
stopped
Code                  : MULE_ERROR-166
--------------------------------------------------------------------------------
Exception stack is:
1. Cannot process event as "getCSVAccountsFlow" is stopped 
(org.mule.api.lifecycle.LifecycleException)

org.mule.construct.AbstractPipeline$ProcessIfPipelineStartedMessageProcessor:440           
(http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/lifecycle/Lifecy
cleException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.lifecycle.LifecycleException: Cannot process event as "getCSVAccountsFlow" is stopped
at org.mule.construct.AbstractPipeline$ProcessIfPipelineStartedMessageProcessor.handleUnaccepted(AbstractPipeline.java:440)
at org.mule.processor.AbstractFilteringMessageProcessor.process(AbstractFilteringMessageProcessor.java:45)
at org.mule.execution.ExceptionToMessagingExceptionExecutionInterceptor.execute(ExceptionToMessagingExceptionExecutionInterceptor.java:24)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

MUnit Test:

<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/mock         
http://www.mulesoft.org/schema/mule/mock/current/mule-mock.xsd 
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">
<munit:config name="munit" doc:name="MUnit configuration" />
<spring:beans>
    <spring:import resource="classpath:accounts.xml" />
</spring:beans>
<munit:test name="accounts-getCSVAccountsFlowTest"
    description="Test">

    <mock:when messageProcessor="File" doc:name="File Input"
        doc:description="Mocks the File processor that accepts a csv file">
        <mock:then-return
            payload="#[getResource('input/accounts.csv').asByteArray()]" />
    </mock:when>

    <flow-ref name="getCSVAccountsFlow" doc:name="Flow-ref to getCSVAccountsFlow" />
    <munit:assert-not-null doc:name="Assert Not Null Payload" />
</munit:test>
</mule>

FLOW:

<flow name="getCSVAccountsFlow" initialState="stopped">
    <file:inbound-endpoint path="src/main/resources/input"
        moveToDirectory="src/main/resources/output" responseTimeout="10000"
        doc:name="File">
        <file:filename-regex-filter pattern=".*csv"
            caseSensitive="false" />
    </file:inbound-endpoint>
    <dw:transform-message metadata:id="6b5dfac1-0410-40c4-b920-d7fdcd60333c" doc:name="Transform Message">
        <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
payload map ((value , index) -> {
  Name: value.Name,
  BillingStreet: value.BillingStreet,
  BillingCity: value.BillingCity,
  BillingState: value.BillingState,
  BillingPostalCode: value.BillingPostalCode,
  BillingCountry: value.BillingCountry
})]]></dw:set-payload>
    </dw:transform-message>
    <foreach doc:name="For Each">
        <logger message="#[payload]" level="DEBUG" doc:name="Logger" />
    </foreach>
    <logger message="#[payload]" level="DEBUG" doc:name="Logger" />
</flow>

Solution

  • MUnit test fails because the Flow's Initial State is stopped.

    <flow name="getCSVAccountsFlow" initialState="stopped">
    

    Set it to empty by:

    1. Select your flow and open its properties
    2. Inside Flow Configuration section, set Initial State to -- Empty --
    3. Re-run MUnit test