Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Camel Testing

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)}

like image 677
Mittal Avatar asked Dec 05 '22 13:12

Mittal


1 Answers

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) 
like image 147
mgyongyosi Avatar answered Jan 14 '23 08:01

mgyongyosi