2016-07-06 28 views
0

我正在嘗試編寫一個騾流的集成測試,從JMS隊列中檢索文件並將其插入到數據庫中。當數據庫不可用時,一個測試場景正在運行異常流程(連接錯誤)。對於這個測試,我想模擬DB連接器並讓它拋出一個異常。穆尼特嘲諷數據庫連接器

我試圖嘲笑這樣的連接器:

@Test 
public void testDBOutageException() throws Exception { 
    MuleEvent event = testEvent(INPUT_VALID_XML); 

    java.net.ConnectException connectException = new java.net.ConnectException("Could not connect to DB!");  
    MessageProcessorMocker dbMocker = whenMessageProcessor("insert").ofNamespace("db");  
    dbMocker.thenThrow(connectException); 

    event = runFlow("thomson-reuters-processFlow", event); 
} 

當運行測試用例的流動上面已成功稱爲但DB連接器沒有嘲笑。它只是調用數據庫並插入數據。這不是我想要達到的。

我的流程是這樣的:

<?xml version="1.0" encoding="UTF-8" ?> 
<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" 
xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" 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" 
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd 
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd 
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd 
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd"> 
    <context:property-placeholder location="classpath:thomson-reuters-process.properties, classpath:thomson-reuters-process-${env}.properties"></context:property-placeholder> 

    <spring:beans> 
    <spring:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <spring:property name="url" value="${JDBC.url}"></spring:property> 
     <spring:property name="username" value="${JDBC.username}"></spring:property> 
     <spring:property name="password" value="${JDBC.password}"></spring:property> 
     <spring:property name="driverClassName" value="${JDBC.driver}"></spring:property> 
     <spring:property name="initialSize" value="5"></spring:property> 
     <spring:property name="testOnBorrow" value="true"></spring:property> 
     <spring:property name="validationQuery" value="${JDBC.validationQuery}"></spring:property> 
     <spring:property name="maxWait" value="60000"></spring:property> 
     <spring:property name="maxActive" value="50"></spring:property> 
     <spring:property name="removeAbandoned" value="true"></spring:property> 
     <spring:property name="removeAbandonedTimeout" value="300"></spring:property> 
     <spring:property name="logAbandoned" value="true"></spring:property> 
    </spring:bean> 
    </spring:beans> 

    <jms:activemq-connector name="Active_MQ" username="${JMS.User}" password="${JMS.Password}" brokerURL="tcp://${JMS.Host}:${JMS.Port}" validateConnections="true" doc:name="Active MQ"> 
    <reconnect frequency="10000" count="10" /> 
    </jms:activemq-connector> 
    <!-- <db:mysql-config name="MySQL_Configuration" doc:name="MySQL Configuration" 
     database="${DB.Database}" host="${DB.Host}" password="${DB.Password}" port="${DB.Port}" 
     user="${DB.User}"></db:mysql-config> --> 
    <db:generic-config name="DB_Config" dataSource-ref="dataSource" doc:name="DB_Config" /> 
    <flow name="thomson-reuters-processFlow"> 
    <jms:inbound-endpoint queue="${JMS.InQueue}" connector-ref="Active_MQ" doc:name="Retrieve from ActiveMQ JMS"></jms:inbound-endpoint> 
    <set-variable variableName="inboundPayload" value="#[payload]" doc:name="Store inboundPayload"></set-variable> 
    <!-- <logger level="INFO" doc:name="Logger"></logger> <mulexml:schema-validation-filter 
      schemaLocations="test-XSD-schema.xsd" returnResult="true" doc:name="Schema 
      Validation"></mulexml:schema-validation-filter> <logger level="INFO" doc:name="Logger"></logger> --> 
    <logger message="LOGGER 1: Inbound file from JMS, payload is: #[message.payloadAs(String)]" level="INFO" doc:name="Logger"></logger> 
    <splitter expression="#[xpath3('/Report/Data/Row[position()&gt;1]',payload,'NODESET')]" doc:name="Split XML to currency dataset"></splitter> 
    <logger message="LOGGER 2: XML was split to forex data set" level="INFO" doc:name="Logger"></logger> 
    <mulexml:dom-to-xml-transformer doc:name="DOM to XML"></mulexml:dom-to-xml-transformer> 
    <collection-aggregator failOnTimeout="true" doc:name="Build collection of individual currency rates"> 
     <expression-message-info-mapping messageIdExpression="#[message.id]" correlationIdExpression="#[message.correlationId]" /> 
    </collection-aggregator> 
    <logger message="LOGGER 3: Combined forex data in collection" level="INFO" doc:name="Logger"></logger> 
    <db:insert config-ref="DB_Config" bulkMode="true" doc:name="Push xml collection to DB" doc:description="dbConnectorInsert"> 
     <db:dynamic-query> 
     <![CDATA[INSERT INTO `exchange_rates`(`CURRENCY`, `DATE`, `REF_CURRENCY`, `EXCHANGE_RATE`,`CREATED_BY`,`CREATE_DATE`,`CHANGED_BY`,`CHANGE_DATE`,`BUYING_RATE`,`SPECIAL_RATE`) VALUES ('#[xpath3('//CURRENCY')]','#[xpath3('//DATE')]','#[xpath3('//REF_CURRENCY')]','#[xpath3('//EXCHANGE_RATE')]','${System.User}','#[server.dateTime.format('yyyy-MM-dd HH:mm:ss')]', '${System.User}','#[server.dateTime.format('yyyy-MM-dd HH:mm:ss')]','#[xpath3('//BUYING_RATE')]','#[xpath3('//SELLING_RATE')]');]]> 
     </db:dynamic-query> 
    </db:insert> 
    <logger message="LOGGER 4: DB Processing completed, payload is: #[message.payloadAs(String)]" level="INFO" doc:name="Logger"></logger> 
    <catch-exception-strategy doc:name="thomson-reuters-processCatch_Exception_Strategy"> 
     <logger level="INFO" doc:name="Logger"></logger> 
     <set-payload value="#[flowVars.inboundPayload]" doc:name="Set Original Inbound Payload"></set-payload> 
     <message-properties-transformer doc:name="Remove MULE_CORRELATION_ID"> 
     <delete-message-property key="MULE_CORRELATION_ID" /> 
     </message-properties-transformer> 
     <logger message="Exception occured, trying to push message to DLQ, payload is: #[message.payloadAs(String)]" level="INFO" doc:name="Logger"></logger> 
     <jms:outbound-endpoint queue="${JMS.DLQ}" connector-ref="Active_MQ" doc:name="Place on DLQ"></jms:outbound-endpoint> 
    </catch-exception-strategy> 
    </flow> 
</mule> 

任何想法,使這項工作?

回答

0

MUnit 1.1.0中存在DB Mock無法運行的錯誤。它已在最近發佈的MUnit 1.2.0版本中得到修復,請嘗試1.2.0。你使用的是哪個版本的MUnit?

+0

我使用MUnit版本3.6.0-RC2 ' 3.6.0-RC2 org.mule.munit munit常見 $ {} munit.version 測試 ' –

+0

有沒有這樣的版本市政噸。選中此項 - https://docs.mulesoft.com/munit/v/1.2.0/munit-maven-support。 –

+0

3.6.2-RC2是MUnit的一箇舊版本,在GA之前,它不被支持。 我建議你遷移到更新版本的MUnit(如果可能的話,最後發佈的版本)。 對於那些來自舊版MUnit版本的用戶有一個遷移指南,你可以看看它: https://docs.mulesoft.com/munit/v/1.0.0/munit-1.0.0-migration -指南 – Dds