【问题标题】:How to write a Unitest case for the apache camel routing in the springboot application如何在 Spring Boot 应用程序中为 Apache Camel 路由编写单元测试用例
【发布时间】:2021-04-30 20:25:02
【问题描述】:

我被指派为使用 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 为应用程序的其他部分编写测试用例。 请帮忙。谢谢。

【问题讨论】:

    标签: unit-testing routes apache-camel spring-boot-test


    【解决方案1】:

    查看有关 Camel 和 SpringBoot 的文档。 JUnit 4 和 5 中有一个 section about Testing

    这是 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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-05
      • 1970-01-01
      • 2019-04-04
      • 2020-05-26
      • 1970-01-01
      • 2020-08-30
      • 1970-01-01
      • 2020-06-14
      • 2020-03-07
      相关资源
      最近更新 更多