Spring Boot Camel Testing

Related searches

I need to test Camel routes in a Spring Boot Application. I've the Spring boot main class with all the necessary beans declared in it. I am using the CamelSpringJUnit4ClassRunner.class. Added my Spring boot main class in @ContextConfiguration as it contains all the configurations. I don't have a separate configuration class.

I 've autowired CamelContext in my Test class:

@Autowired
CamelContext camelContext;

But the test fails with the error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:

No qualifying bean of type 'org.apache.camel.CamelContext' available: expected at least 1 bean which qualifies as autowire candidate.

Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Try to use the CamelSpringBootRunner.class as the runner and add the @SpringBootTest annotation to the test class.

Example from the Camel repository

UPDATE (based on your comment)

If you change your bootstrapper class to SpringBootTestContextBootstrapper then it should work:

@BootstrapWith(SpringBootTestContextBootstrapper.class)

The equivalent configuration as you have but in this case you don't need to add the ContextConfiguration and the BootstrapWith annotation:

@RunWith(CamelSpringBootRunner.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("log:*")
@DisableJmx(false)
@SpringBootTest(classes = MyClass.class) 

Spring boot and camel testing with @SpringBootTest, For example, we will implement a test of the route specified previously. @ RunWith(CamelSpringBootRunner.class). I need to test Camel routes in a Spring Boot Application. I've the Spring boot main class with all the necessary beans declared in it. I am using the CamelSpringJUnit4ClassRunner.class. Added my Spring boot main class in @ContextConfiguration as it contains all the configurations. I don't have a separate configuration class.

With Camel 3.1 Spring Boot 2.2.5 and JUnit5, while also setting test application properties:

@CamelSpringBootTest
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(properties = "spring.cloud.consul.enabled=false")
public class CamelRouteTest {

  @Autowired
  private TestRestTemplate restTemplate;

  @Autowired
  private CamelContext camelContext;

  @EndpointInject("mock:bean:userService")
  private MockEndpoint mockUserService;

  private User user;

  @BeforeEach
  public void setUp() throws Exception {
    AdviceWithRouteBuilder.adviceWith(camelContext, "getUsersRoute", a -> {
      a.mockEndpointsAndSkip("bean:userService*");
    });

    user = new User();
    user.setId(1);
    user.setName("Jane");

    mockUserService.returnReplyBody(constant(new User[] {user}));
  }

  @Test
  public void callsRestWithMock() {
    ResponseEntity<User[]> response = restTemplate.getForEntity("/rest/users", User[].class);
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
    User[] s = response.getBody();
    assertThat(s).contains(user);
  }

  @Test
  public void callsDirectRouteWithMock() throws Exception {
    User[] users = DefaultFluentProducerTemplate.on(camelContext)
        .to("direct:getusers")
        .request(User[].class);
    assertThat(users).contains(user);
  }

  @Test
  public void camelStarts() {
    assertEquals(ServiceStatus.Started, camelContext.getStatus());
    assertThat(camelContext.getRoutes()).hasSizeGreaterThan(0);
  }
}

Assuming a RouteBuilder:

@Component
public class CamelRouter extends RouteBuilder {

  @Value("${server.port}")
  private int serverPort;

  @Override
  public void configure() throws Exception {
    restConfiguration()
        .contextPath("/rest")
        .component("servlet")
        .apiContextPath("/api-doc")
        .port(serverPort)
        .bindingMode(RestBindingMode.json)
        .dataFormatProperty("prettyPrint", "true");

    rest("/users")
        .consumes("application/json")
        .produces("application/json")
        .get()
        .outType(User[].class).to("direct:getusers");

    from("direct:getusers").routeId("getUsersRoute")
        .log("Get users")
        .to("bean:userService?method=listUsers");
  }
}

and application.yml:

camel:
  component:
    servlet:
      mapping:
        context-path: /rest/*
  springboot:
    name: MyCamel

Spring Boot and Apache Camel, This article will cover how to write a test for Apache Camel Rest DSL with Spring Boot. Many people find Apache Camel unit testing a big� Testing is a crucial part of any development or integration work. The Spring Framework offers a number of features that makes it easy to test while using Spring for Inversion of Control which works with JUnit 3.x or JUnit 4.x.

just enable @EnableAutoConfiguration it will work

Unit Testing for Camel Rest DSL and Spring Boot, When you have multiple Camel routes in different RouteBuilder classes, Camel on Spring Boot will include all these routes. But you may want to write a unit test� Spring Boot Tutorial for Beginners - Learn Spring Boot in simple steps from basic to advanced concepts with tutorials including Introduction, Quick Start, Bootstrapping, Tomcat Deployment, Build Systems, Code Structure, Spring Beans and Dependency Injection, Runners, Application Properties, Logging, Building RESTful Web Services, Exception Handling, Interceptor, Servlet Filter, Tomcat Port

Chapter 9: Testing - Camel in Action, At willhaben, we use Apache Camel together with Spring Boot for many different projects. However, writing unit tests for these Camel routes is� testCompile ('org.springframework.boot:spring-boot-starter-test') testCompile ( 'org.apache.camel:camel-test-spring:2.20.0' ) In order to have a faster project setup from scratch, you can always

Testing Apache Camel Applications with Spring Boot, In this tutorial we do unit testing for Apache Camel Spring Application using CamelSpringTestSupport. and FileIdempotentRepository Spring Boot + Apache Camel JDBC

Testing is the crucial weapon to ensure that things work as you would expect. Camel is a Java library so you can easily wire up tests in whatever unit testing framework you use (JUnit 3.x (deprecated) or 4.x).

Comments
  • Thank you.The below exception is still there.
  • Can you post the relevant part from your test class (class declaration, ContextConfiguration settings)?
  • @RunWith(CamelSpringJUnit4ClassRunner.class) @BootstrapWith(CamelTestContextBootstrapper.class) @ContextConfiguration(MyClass.class) @DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) @MockEndpoints("log:*") @DisableJmx(false) public class CamelSpringJUnit4ClassRunnerPlainTest { @Autowired protected CamelContext camelContext; ......... } Let MyClass be my SpringBoot start up class with the main method and all the other beans defined in it.
  • I tried with the @SpringBootTest .But its still throwing exception on autowiring CamelContext.