unit testing - Mock a message from a 3rd party system in Mule using MUnit -
i'm writing test suite (using munit) mule application processing new data coming instance of magento. 1 of flows polling magento new customers , message receives of type: com.magento.api.customercustomerentity
i'm wondering how i'd mock in test case, when magento message processor called can return payload of same type , make appropriate assertations?
currently munit test looks follows:
<mock:config name="mock_magentotosalesforce" doc:name="mock configuration"/> <spring:beans> <spring:import resource="classpath:magentotosalesforce.xml"/> <spring:bean id="mybean" name="mybean" class="com.magento.api.customercustomerentity"> <spring:property name="email" value="test@test.com"/> </spring:bean> </spring:beans> <munit:test name="magentotosalesforce-test-getcustomersflowtest" description="test"> <mock:when config-ref="mock_magentotosalesforce" messageprocessor=".*:.*" doc:name="mock"> <mock:with-attributes> <mock:with-attribute wherevalue-ref="#[string:get new customers]" name="doc:name"/> </mock:with-attributes> <mock:then-return payload-ref="#[app.registry.mybean]"/> </mock:when> <flow-ref name="getcustomers" doc:name="flow-ref getcustomers"/> </munit:test>
and flow i'm trying test is:
<flow name="getcustomers" processingstrategy="synchronous"> <poll doc:name="poll"> <fixed-frequency-scheduler frequency="30" timeunit="seconds"/> <watermark variable="watermark" default-expression="#[new org.mule.el.datetime.datetime().plusyears(-30)]" update-expression="#[new org.mule.el.datetime.datetime().plusyears(-0)]" selector-expression="#[new org.mule.el.datetime.datetime(payload.created_at, 'yyyy-mm-dd hh:mm:ss')]"/> <magento:list-customers config-ref="magento" filter="dsql:select confirmation,created_at,created_in,customer_id,dob,email,firstname,group_id,increment_id,lastname,middlename,password_hash,prefix,store_id,suffix,taxvat,updated_at,website_id customercustomerentity updated_at > '#[flowvars.watermark]'" doc:name="get new customers"/> </poll> <foreach doc:name="for each"> <data-mapper:transform config-ref="magentocustomer_to_salesforcecontact" doc:name="map customer sfdc contact"> <data-mapper:input-arguments> <data-mapper:input-argument key="contactsource">magento</data-mapper:input-argument> </data-mapper:input-arguments> </data-mapper:transform> <flow-ref name="upsertsalesforcecontactflow" doc:name="upsertsalesforcecontactflow"/> </foreach> </flow>
update following ryan's answer:
changed expression return payload of #[ent = new com.magento.api.customercustomerentity(); ent.setemail('test@test.com'); return [ent];]
- note, changed method setemail
match documentation here. error is:
error 2015-06-22 09:58:34,719 [main] org.mule.exception.defaultmessagingexceptionstrategy: ******************************************************************************** message : object "org.mule.transport.nullpayload" not of correct type. must of type "{interface java.lang.iterable,interface java.util.iterator,interface org.mule.routing.messagesequence,interface java.util.collection}" (java.lang.illegalargumentexception). message payload of type: nullpayload code : mule_error--2 -------------------------------------------------------------------------------- exception stack is: 1. object "org.mule.transport.nullpayload" not of correct type. must of type "{interface java.lang.iterable,interface java.util.iterator,interface org.mule.routing.messagesequence,interface java.util.collection}" (java.lang.illegalargumentexception) org.mule.util.collection.eventtomessagesequencesplittingstrategy:64 (null) 2. object "org.mule.transport.nullpayload" not of correct type. must of type "{interface java.lang.iterable,interface java.util.iterator,interface org.mule.routing.messagesequence,interface java.util.collection}" (java.lang.illegalargumentexception). message payload of type: nullpayload (org.mule.api.messagingexception) org.mule.execution.exceptiontomessagingexceptionexecutioninterceptor:32 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/messagingexception.html) -------------------------------------------------------------------------------- root exception stack trace: java.lang.illegalargumentexception: object "org.mule.transport.nullpayload" not of correct type. must of type "{interface java.lang.iterable,interface java.util.iterator,interface org.mule.routing.messagesequence,interface java.util.collection}" @ org.mule.util.collection.eventtomessagesequencesplittingstrategy.split(eventtomessagesequencesplittingstrategy.java:64) @ org.mule.util.collection.eventtomessagesequencesplittingstrategy.split(eventtomessagesequencesplittingstrategy.java:25) @ org.mule.routing.collectionsplitter.splitmessageintosequence(collectionsplitter.java:29) + 3 more (set debug level logging or '-dmule.verbose.exceptions=true' everything) ********************************************************************************
one way build object using constructor or properties/setters.
from docs: http://mulesoft.github.io/magento-connector/2.1.2/java/com/magento/api/customercustomerentity.html
<mock:then-return payload-ref="#[ent = new com.magento.api.customercustomerentity(); ent.email('test@test.com'); return ent;]"/>
you can create these objects reusable spring beans , reference them mel.
<bean class="com.magento.api.customercustomerentity" id="myentitywithemail"> <property name="email" value="test@test.com" /> </bean> <mock:then-return payload-ref="#[app.registry.myentitywithemail]"/>
after update can see sre using foreach expects collection or iterable. can return collection of custom object in mel using: [] example:
#[ent = new com.magento.api.customercustomerentity(); ent.email('test@test.com'); return [ent];]
more on mel here: https://developer.mulesoft.com/docs/display/current/mule+expression+language+mel
or again can use spring return list:
<util:list id="entities"> <ref bean="myentitywithemail" /> </util:list>
Comments
Post a Comment