Spring boot and camel testing with @SpringBootTest
Asked Answered
N

2

7

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

Negotiation answered 13/12, 2017 at 15:12 Comment(2)
Post the exception/ error/ stacktrace!Negro
Unfortunaty error is junit assert error , that assert is not satisfiedNegotiation
N
11

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();
  }
}
Negotiation answered 14/12, 2017 at 15:1 Comment(2)
I don't know why but this does not work for me? Can you please specify how you are testing your class MyRoute in test 2?Paulino
@Sandeep Mandori Message is sent to the file:in that is defined in the route that you are testing as a from. Message is that at the end sent in mock:file:out as we can monitor mock enpoint and then mockCamel.assertIsSatisfied(); will check if the endpoint is satisfield ( message count is 1 )Negotiation
B
6

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.

Belen answered 14/12, 2017 at 11:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.