You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by dancerjohn <wi...@gmail.com> on 2017/05/30 18:30:09 UTC

Spring Boot startup not working in JUnit

I have a Spring Boot application with camel routes that starts up and runs
fine in Tomcat (it is a web app). However I am having real issues getting it
to run as a unit test. I have an @Configuration file that has 3 RouteBuilder
beans. I do not explicitly define a CamelContext been and instead let
camel-spring-boot do that for me (works when deployed).

However, when I try to use this as part of a unit test the route does not
get started up and if I try to @Autowire the CamelContext I get a no such
bean exception.

Here is my test 

@RunWith(SpringJUnit4ClassRunner.class)
@BootstrapWith(CamelTestContextBootstrapper.class)
@SpringApplicationConfiguration(classes={MyConfiguration.class})
@EnableAutoConfiguration
public class MyTest{

@Autowired
public CamelContext context;

@Producer(uri="direct:blah")
public ProducerTemplate template;

@Test
public void mytest(){
template.sendBody("blah");
}

I have another project where I explicitly created the CamelContext in a
spring XML file and that worked really well but would like to avoid that
since I don't need it with Spring Boot.



--
View this message in context: http://camel.465427.n5.nabble.com/Spring-Boot-startup-not-working-in-JUnit-tp5801391.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Spring Boot startup not working in JUnit

Posted by owain <ow...@integration.technology>.
This is how my tests are set up

@RunWith(CamelSpringBootRunner.class)
@MockEndpoints
@UseAdviceWith
@SpringBootTest(classes =
mydomain.springbootcamel.SpringBootCamelExamples.class)
public class RouteBTests {

  @Autowired
  private CamelContext camelContext;

  @Autowired
  private ProducerTemplate template;

  @Test
  public void whereIsBuckinghamPalaceTest() throws Exception {


I have all of my routes annotated to pick up rather than in a configuration
class.

package mydomain.springbootcamel;

import org.apache.camel.*;
import org.apache.camel.builder.*;

import org.springframework.stereotype.Component;

@Component
public class RouteB extends RouteBuilder{

  @Override
  public void configure() throws Exception {

    from("direct:RouteB")
            .routeId("RouteB")
            .log(LoggingLevel.INFO, "Received Body:
${body}").id("log-input")
            .setBody().simple("Who lives at ${body}")
            .log(LoggingLevel.INFO, "Converted Body:
${body}").id("log-result")
    ;
  }
}

Here is a repo to browse. 
https://github.com/owain68/camel-spring-boot-examples

HTH.




--
View this message in context: http://camel.465427.n5.nabble.com/Spring-Boot-startup-not-working-in-JUnit-tp5801391p5801397.html
Sent from the Camel - Users mailing list archive at Nabble.com.

Re: Spring Boot startup not working in JUnit

Posted by dancerjohn <wi...@gmail.com>.
I updated to Spring Boot 1.5.3 and used SpringBootTest and that works.
Thanks!



--
View this message in context: http://camel.465427.n5.nabble.com/Spring-Boot-startup-not-working-in-JUnit-tp5801391p5801502.html
Sent from the Camel - Users mailing list archive at Nabble.com.