springboot应用中apachecamel路由如何写Unitest案例

问题描述

我被指派为使用 Apache Camel 进行路由的 Springboot 应用程序编写单元测试。

下面是一个简单的路由类。

    @Component
    public class MyRouteBuilder extends RouteBuilder
    {
       @Override
       public void configure() throws Exception {
          super.configure();
          from("direct:encrypt").bean(ProcessData.class,"process(${exchange})").end();
       }    
    }

如何为此编写单元测试。该应用程序使用 Mockito 为应用程序的其他部分编写测试用例。 请帮忙。谢谢。

解决方法

查看有关 Camel 和 SpringBoot 的文档。 JUnit 4 和 5 有一个 https://abldojo.services.progress.com/?shareId=6013b9f19585066c219797fa

这是 Camel 3、SpringBoot 2 和 JUnit 5 的示例

@CamelSpringBootTest
@SpringBootTest
class MyRouteTest {
    @Autowired
    private CamelContext camelContext;
    @Autowired
    private ProducerTemplate producer;
    private MockEndpoint mockEndpoint;

    @BeforeEach
    public void doBeforeEveryTest() {
        MockEndpoint.resetMocks(camelContext);
    }

    @Test
    void testWhateverRouteDetail() throws Exception {
        mockEndpoint = camelContext.getEndpoint("mock:output",MockEndpoint.class);
        mockEndpoint.expectedBodiesReceivedInAnyOrder(yourExpectedBody);

        producer.sendBodyAndHeader("direct:encrypt",myBodyContent,headerName,headerValue);
        mockEndpoint.assertIsSatisfied();
    }
}