簡體   English   中英

在 Spring 引導中測試駱駝處理器

[英]Test Camel Processor in Spring Boot

我正在嘗試在 Spring Boot 中測試 Camel 處理器。 但是,即使遵循 Camel in Action 和其他 SO 答案的指導,我也會遇到很多例外情況。

我正在使用駱駝 3.0.0-M4

例如,這是我的測試:

@SpringBootTest
@BootstrapWith(SpringBootTestContextBootstrapper.class)
public class MyProcessorTest extends CamelTestSupport {

    @Produce
    ProducerTemplate template;

    @EndpointInject(uri = "mock:out")
    private MockEndpoint resultEndpoint;

    @Before
    public void setUp() throws Exception {
        super.setUp();
    }

    @BeforeClass
    public static void stopCamelStart() {
        SpringCamelContext.setNoStart(true);
    }

    @AfterClass
    public static void enableCamelStart() {
        SpringCamelContext.setNoStart(false);
    }

    @Before
    protected RouteBuilder createRouteBuilder() throws Exception {
        return new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("seda:test")
                        .process(new MyProcessor())
                        .to("mock:out");
            }
        };
    }

    @Test
    public void testPrepend() throws Exception {
        Map<String, String> body = new HashMap<String, String>();
        body.put("test", "body");

        template.sendBody("seda:test", body);
        String result = resultEndpoint.getExchanges().get(0).getIn().getBody(String.class);

    }
}

我得到一個java.lang.ArrayIndexOutOfBoundsException

如果我嘗試啟動駱駝上下文context.start(); 我得到一個java.lang.NullPointerException

如果我交換路由,我將正文發送到template.sendBody("mock:out", body); 沒有進行任何處理。

如果我從seda:test更改為direct:test我會得到一個運行速度非常慢的測試和org.apache.camel.component.direct.DirectConsumerNotAvailableException: No consumers available on endpoint: direct://test

我哪里錯了??

在第一個視圖中,您有兩個標有@Before的方法 JUnit 完全不保證兩者的執行順序 所以也許這會造成麻煩。


但是,我認為不需要設置 Camel Route Test 來測試 Processor

我強烈建議用 POJO替換您的處理器(它們很笨拙且難以測試),並用.bean而不是.processor來調用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM