在Spring Boot中测试骆驼处理器

时间:2019-10-02 13:50:43

标签: java spring spring-boot apache-camel

我正在尝试在Spring Boot中测试骆驼处理器。但是,即使遵循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

我要去哪里错了?

1 个答案:

答案 0 :(得分:1)

在第一个视图上,您​​有两种标记为@Before 的方法。 JUnit完全不能保证两者的执行顺序。因此,这可能会引起麻烦。


但是,我看不到需要设置骆驼路线测试来测试处理器

我强烈建议您使用POJO 替换处理器(它们笨拙且难以测试)并用.bean代替.processor