Spring boot and camel testing with @SpringBootTest

16,546

Solution 1

Here is what worked at the end :

@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@MockEndpoints
public class MyRouteTest2 {

  @Autowired
  private ProducerTemplate producerTemplate;

  @EndpointInject(uri = "mock:file:out")
  private MockEndpoint mockCamel;

  @Test
  public void test() throws InterruptedException {
    String body = "Camel";
    mockCamel.expectedMessageCount(1);

    producerTemplate.sendBody("file:in", body);

    mockCamel.assertIsSatisfied();
  }
}

Solution 2

Try adding the annotation @RunWith(CamelSpringBootRunner.class). According to docs:

An implementation bringing the functionality of CamelSpringTestSupport to Spring Boot Test based test cases. This approach allows developers to implement tests for their Spring Boot based applications/routes using the typical Spring Test conventions for test development.

Also, consider adding @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) and @DisableJmx(true). The first one clears the Spring Context after each test method, that will avoid any consequences left from other test in the same test case (like failing a test with apparently no reason).

The later, will disable the JMX since you shouldn't need it on your tests.

The official docs has more information about running Apache Camel with Spring Boot. Here a example extract from it:

@ActiveProfiles("test")
@RunWith(CamelSpringBootRunner.class)
@SpringBootTest
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@DisableJmx(true)
public class MyRouteTest extends CamelTestSupport {

    @Autowired
    private CamelContext camelContext;

    @Override
    protected CamelContext createCamelContext() throws Exception {
        return camelContext;
    }

    @EndpointInject(uri = "direct:myEndpoint")
    private ProducerTemplate endpoint;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        RouteDefinition definition = context().getRouteDefinitions().get(0);
        definition.adviceWith(context(), new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                onException(Exception.class).maximumRedeliveries(0);
            }
        });
    }

    @Override
    public String isMockEndpointsAndSkip() {
            return "myEndpoint:put*";
    }

    @Test
    public void shouldSucceed() throws Exception {
        assertNotNull(camelContext);
        assertNotNull(endpoint);

        String expectedValue = "expectedValue";
        MockEndpoint mock = getMockEndpoint("mock:myEndpoint:put");
        mock.expectedMessageCount(1);
        mock.allMessages().body().isEqualTo(expectedValue);
        mock.allMessages().header(MY_HEADER).isEqualTo("testHeader");
        endpoint.sendBodyAndHeader("test", MY_HEADER, "testHeader");

        mock.assertIsSatisfied();
    }
}

Hope that helps.

Share:
16,546
bijesanu
Author by

bijesanu

Updated on July 13, 2022

Comments

  • bijesanu
    bijesanu almost 2 years

    I have spring boot app, with spring boot of version 1.5.8 and there camel 2.20.1

    Simple route :

    @Component
    public class MyRoute extends RouteBuilder {
    
      public static final String IN = "file://in";
    
      public static final String OUT = "file://out";
    
      @Override
      public void configure() throws Exception {
        from(IN).routeId("myId").to(OUT);
      }
    }
    

    Und simple test :

    //@SpringBootTest
    public class MyRouteTest extends CamelTestSupport {
    
    
          @Produce(uri = MyRoute.IN)
          private ProducerTemplate producerTemplate;
    
          @EndpointInject(uri = "mock:file:out")
          private MockEndpoint mockEndpointOut;
    
          @Override
          public String isMockEndpoints() {
            return "*";
          }
    
          @Test
          public void simpleTest() throws Exception {
            mockEndpointOut.expectedMessageCount(1);
            producerTemplate.sendBody("Test");
            mockEndpointOut.assertIsSatisfied();
          }
    
          @Override
          protected RoutesBuilder createRouteBuilder() throws Exception {
            return new MyRoute();
          }
    
        }
    

    When I run this test it runs fine and I get one message and the endpoint is satisfied. If I add @SpringBootTest annotation, the test fails? Why ? Also without annotation when I run maven clean install it also fails :(

    Anyone have any idea what is this annotation does to camel test, or how can I adjust it so that it works?

    Thx