Search code examples
javaspring-bootapache-camelspring-boot-testspring-camel

Spring boot and camel testing with @SpringBootTest


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


Solution

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