Spring Boot Apache Camel Routes testing

后端 未结 4 442
粉色の甜心
粉色の甜心 2020-12-06 15:57

I have a Springboot application, where I have some camel routes configured.

public class CamelConfig {
private static final Logger LOG = LoggerFactory.getLog         


        
4条回答
  •  独厮守ぢ
    2020-12-06 16:13

    For one route with MQ and Spring Boot like this:

        @Component
        public class InboundRoute extends RouteBuilder {
        
          @Override
          public void configure() {
            JaxbDataFormat personDataFormat = new JaxbDataFormat();
            personDataFormat.setContextPath(Person.class.getPackage().getName());
            personDataFormat.setPrettyPrint(true);
            from("direct:start").id("InboundRoute")
                .log("inbound route")
                .marshal(personDataFormat)
                .to("log:com.company.app?showAll=true&multiline=true")
                .convertBodyTo(String.class)
                .inOnly("mq:q.empi.deim.in")
                .transform(constant("DONE"));
          }
        }
    

    I use adviceWith in order to replace the endpoint and use only mocks:

        @RunWith(CamelSpringBootRunner.class)
        @UseAdviceWith
        @SpringBootTest(classes = InboundApp.class)
        @MockEndpoints("mock:a")
        public class InboundRouteCamelTest {
        
          @EndpointInject(uri = "mock:a")
          private MockEndpoint mock;
        
          @Produce(uri = "direct:start")
          private ProducerTemplate template;
        
          @Autowired
          private CamelContext context;
        
          @Test
          public void whenInboundRouteIsCalled_thenSuccess() throws Exception {
            mock.expectedMinimumMessageCount(1);
            RouteDefinition route = context.getRouteDefinition("InboundRoute");
            route.adviceWith(context, new AdviceWithRouteBuilder() {
              @Override
              public void configure() {
                weaveByToUri("mq:q.empi.deim.in").replace().to("mock:a");
              }
            });
            context.start();
        
            String response = (String) template.requestBodyAndHeader("direct:start",
                getSampleMessage("/SimplePatient.xml"), Exchange.CONTENT_TYPE, MediaType.APPLICATION_XML);
        
            assertThat(response).isEqualTo("DONE");
            mock.assertIsSatisfied();
          }
        
          private String getSampleMessage(String filename) throws Exception {
            return IOUtils
                .toString(this.getClass().getResourceAsStream(filename), StandardCharsets.UTF_8.name());
          }
        }
    

    I use the following dependencies: Spring Boot 2.1.4-RELEASE and Camel 2.23.2. The complete source code is available on Github.

提交回复
热议问题