猿问

在 Spring Boot 中测试 Camel 处理器

我正在尝试在 Spring Boot 中测试 Camel 处理器。然而,即使遵循 Camel in Action 和其他答案的指导,我也会遇到很多异常。


我使用的是骆驼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


我哪里错了??


幕布斯7119047
浏览 77回答 1
1回答

MMMHUHU

在第一个视图中,您有两种标有 的方法@Before。JUnit 根本不保证两者的执行顺序。所以也许这会造成麻烦。但是,我认为没有必要设置 Camel Route Test 来测试 Processor。我强烈建议用POJO替换您的处理器(它们很笨拙且难以测试),并用 来调用它而不是..bean.processorBean 非常容易测试您可以使用超级快的普通 JUnit 测试您可以将消息部分注入到您的方法中,而不是通过 Camel Exchange 进行导航
随时随地看视频慕课网APP

相关分类

Java
我要回答