2017-08-27 98 views
0

我正在使用Spring Boot,並且我剛剛添加了駱駝。簡單的駱駝測試失敗,沒有收到消息

我有一個簡單的駱駝路線設置:

import org.apache.camel.builder.RouteBuilder; 
import org.springframework.stereotype.Component; 

@Component 
public class MyRoute extends RouteBuilder { 

    @Override 
    public void configure() throws Exception { 
    from("file://in").to("file://out"); 
    } 
} 

當我嘗試用這條路線創建簡單的測試:

@RunWith(CamelSpringBootRunner.class) 
@SpringBootTest 
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) 
public class MyRouteTest extends CamelTestSupport { 

    @Autowired 
    private CamelContext camelContext; 

    @Produce(uri = "file://in") 
    private ProducerTemplate producerTemplate; 

    @EndpointInject(uri = "mock:file://out") 
    private MockEndpoint mockEndpoint; 

    @Test 
    public void routeTest() throws Exception { 
    mockEndpoint.expectedMessageCount(1); 
    producerTemplate.sendBody("Test"); 
    mockEndpoint.assertIsSatisfied(); 
    } 
} 

它失敗

mock://file://out Received message count. Expected: <1> but was: <0> 

不知道這裏可能是一個問題。我有一個生成器模板,它具有uri作爲我從一開始的路線,並且正在用EndpointInject和模擬uri來嘲弄端點。

+0

Configur e [AdviceWith](http://camel.apache.org/advicewith.html) 詳細示例:http://opensourceconnections.com/blog/2014/04/24/correctly-using-camels-advicewith-in-unit -tests/ – mgyongyosi

+0

嘗試過,同樣的錯誤,奇怪的 – George96

回答

0

您需要添加

@Override 
    public String isMockEndpoints() { 
    return "*"; 
    } 

這應該嘲笑所有enpoints,然後你可以使用Mock:文件:出例如

+0

thx,工作! 仍然不確定爲什麼沒有使用@SpringBootTest註解 – George96

0

如果我沒有誤會,你是在嘲笑你的輸出端點,但你的端點端點是文件端點。發送消息時,您需要放棄一條消息,以確定文件端點正在輪詢。否則,你也需要嘲笑它。

+0

sry但我很困惑,不太確定你的想法是什麼。我認爲這producerTemplate.sendBody(「測試」);會在路線中輸入一條消息? – George96

+0

是的,但是您的路由正在監聽文件端點而不是直接端點。你的錯誤是說,期待一條消息,但沒有收到任何消息。將一個模擬端點添加到您的文件:in並將消息發送到端點mock:file:in。或者使用replaceFromWith()。見這裏https://stackoverflow.com/questions/35032612/camel-mock-test-using-file-as-input –

1

固定的,但不是100%

如果我改變路線,從真正的

from("file://in").to("file://out"); 

from("file://in").to("mock:out"); 

而且在我的測試覆蓋

@Override 
    protected RoutesBuilder createRouteBuilder() throws Exception { 
    return new MyRoute(); 
    } 

創建特定路線

和最奇怪的!不得不刪除:

@SpringBootTest 

,之後

private CamelContext camelContext; 

然後它開始工作!

但不幸的是不是我需要的東西,仍然有東西需要修復,我想用我真正的prod路線!

from("file://in").to("file://out"); 

如果可能的話沒有在路線使用的提醒,只是嘲笑它,試圖用 模擬:文件://了測試,但它沒有工作:( 而且,它不與@ WORK ?!SpringBootTest ???很奇怪