JDBC - neilcatalan/MUnit-Implementation GitHub Wiki
For the JDBC implementation, we make use of the OOTB Mock DB Server component in Anypoint Studio, which will make easier for us to have a more close to reality testing and it will help us not to define complex mock definitions. For this case Anypoint Studio make use of an embedded DB Server called H2 Java SQL Database. If I'm not mistaken the required library is automatically imported by Anypoint Studio so you don't have to do any configuration.
Below is our sample JDBC outbound endpoint which simply have a SELECT SQL statement.
<flow name="testDbFlow">
<db:select config-ref="Generic_Database_Configuration" doc:name="Database">
<db:parameterized-query><![CDATA[SELECT * FROM jobtitlelookup WHERE jobtitleid = #[variable:jobid]]]></db:parameterized-query>
</db:select>
<choice doc:name="Choice">
<when expression="#[payload.size()>0]">
<set-payload value="#[payload[0].get('jobtitle')]" doc:name="Set Payload"></set-payload>
</when>
<otherwise>
<set-payload value="#['HIR']" doc:name="Set Payload"></set-payload>
</otherwise>
</choice>
<set-variable value="job" variableName="#[payload]" doc:name="Variable"></set-variable>
</flow>
Below is the test suite for the configuration above:
<munit:config mock-connectors="false" doc:name="MUnit configuration"/>
<dbserver:config name="DB_Server" database="inMemorySampleDB" sqlFile="DB_Structure.sql" doc:name="DB Server"/>
<spring:beans>
<spring:import resource="classpath:globalconfig.xml"/>
<spring:import resource="classpath:jdbc.xml"/>
</spring:beans>
<munit:test name="db-test-suite-case1Test" description="Test">
<munit:set payload="#[]" doc:name="Create a Variable">
<munit:invocation-properties>
<munit:invocation-property key="jobid" value="1"/>
</munit:invocation-properties>
</munit:set>
<flow-ref name="testDbFlow" doc:name="Flow-ref to testDbFlow"/>
</munit:test>
<munit:test name="db-test-suite-case2Test" description="MUnit Test">
<dbserver:execute config-ref="DB_Server" sql="INSERT INTO jobtitlelookup (jobtitleid) VALUES (2)" doc:name="DB Server"/>
<munit:set payload="#[]" doc:name="Create a Variable">
<munit:invocation-properties>
<munit:invocation-property key="jobid" value="2"/>
</munit:invocation-properties>
</munit:set>
<flow-ref name="testDbFlow" doc:name="Flow-ref to testDbFlow"/>
</munit:test>
<munit:after-suite name="after.suite" description="Stopping DB server">
<dbserver:stop-db-server config-ref="DB_Server" doc:name="DB Server"/>
</munit:after-suite>