2016-11-14 53 views
-1

我有以下春天路線:彈簧試驗駱駝基於內容的路由

<camelContext xmlns="http://camel.apache.org/schema/spring"> 
     <route> 
      <from uri="activemq:topic:inbox" /> 
      <choice> 
       <when> 
        <simple>${in.header.Value}</simple> 
        <log message="Cc: ${in.header.Value}" /> 
       </when> 
      </choice> 
      <to uri="mock:result" /> 
     </route> 
</camelContext> 

我必須使用彈簧試驗(CamelSpringJUnit4ClassRunner)的要求,但我發現很容易理解如何測試條件實例與Java DSL。我的測試類是這樣的:

@RunWith(CamelSpringJUnit4ClassRunner.class) 
@BootstrapWith(CamelTestContextBootstrapper.class) 
@ContextConfiguration(locations = "file:src/main/resources/META-INF/spring/camel-context-activemq-embedded.xml") 
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) 
@MockEndpoints("log:*") 
@DisableJmx(false) 

public class MyTest{ 

    private Logger LOG = LogManager.getLogger(MyTest.class.getName()); 

    protected Exchange exchange; 
    protected CustomComponent customComponent= new CustomComponent(); 

    @Produce(uri = "activemq:topic:inbox") 
    protected ProducerTemplate template; 

    @EndpointInject(uri = "mock:result") 
    protected MockEndpoint resultEndpoint; 

@Test 
    public void tes1() throws InterruptedException { 
     String headerValue= MyComponent.Value; 
     EmailAddress recipients = new EmailAddress("[email protected]"); 
     template.sendBodyAndHeader("activemq:topic:inbox", recipients.toString(), headerValue); 
     resultEndpoint.expectedBodiesReceived(headerValue); 
     resultEndpoint.expectedHeaderReceived("header value", headerValue); 
     resultEndpoint.expectedMessageCount(1); 
    } 

我努力理解如何測試由CBR所規定的實際情況,但更重要的是,我懷疑這是否是即便是爲了測試它的正確方法。 MyComponent.VALUEConstant是在我的自定義組件中指定的屬性,上面的測試實際上是通過的。但是,如果我使用組件上的其他屬性實例化headerValue,並且因此條件應該失敗,則測試會通過。你能幫忙嗎?

謝謝

回答

1

嗯,我能看到的第一件事情是,你的簡單表達缺少一個比較 - 這大概應該是${in.header.value} == 'wanted value'

就測試而言 - 這實際上取決於測試類型。在這裏,你在做集成測試,所以我想驗證效果如預期 - DB改變它應有的方式等,但因爲你的路由僅做一些記錄,然後我會改變:

<log message="Cc: ${in.header.Value}" /> 

<log message="Cc: ${in.header.Value}" /> 
<to uri="mock:choice-triggered" /> 

然後驗證mock:choice-triggered端點收到消息(或根據情況不同)。但是在對現實世界路線的測試中,您可能需要驗證某些數據已插入到數據庫中,或者某些消息已發佈到MQ或已發送電子郵件。

就你的常數而言,我建議你使用外部化屬性 - CamelSpring都對屬性佔位符有很好的支持,我建議你給他們一個嘗試。

+0

嗨,謝謝你的幫助。我相信我對簡單表達式本身非常困惑,因爲Value是我自定義組件中的一個實際屬性(僅由我部分編寫)只映射到「Value」。所以,如果我錯了,請糾正我的錯誤,我相信我需要用實際的電子郵件地址創建該值的實例,然後通過sendBodyAndHeader發送它,對吧? – paranza

+0

是的,我以爲自己需要有一個端點(模擬)來驗證消息是否被接收(在之內)。但是,我無法改變春季路線。有沒有什麼方法(我真的在尋找)注入另一個模擬,但只有在測試中? – paranza

+0

我的路線中可以有多個模擬端點嗎?我有一些簡單的日誌:我想驗證的消息以及選擇條件。 – paranza