Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
@ApplicationScoped
@RunWith(CamelCdiRunner.class)
public class CamelCdiTest {
 
    int counter;

    @Test
    @Order(1)
    public void firstTestMethod() {
        counter++;
    }
 
    @Test
    @Order(2)
    public void secondTestMethod() {
        assertEquals(counter, 1);
    }
}

 

In case you need to add additional test beans, you can use the @Beans annotation provided by Camel CDI test. For example, if you need to add a route to your Camel context, instead of declaring a RouteBuilder bean with a nested class, you can declare a managed bean, e.g.:

...

Code Block
languagejava
@RunWith(CamelCdiRunner.class)
@Beans(alternatives = AlternativeBean.class)
public class CamelCdiTest {

    @Test
    public void testAlternativeBean(@Uri("direct:in") ProducerTemplate producer
                                    @Uri("mock:out") MockEndpoint mock) throws InterruptedException {
        mock.expectedMessageCount(1);
        mock.expectedBodiesReceived("test with alternative bean!");

        producer.sendBody("test");

        MockEndpoint.assertIsSatisfied(1L, TimeUnit.SECONDS, mock);
    }

    static class TestRoute extends RouteBuilder {

        @Override
        public void configure() {
            from("direct:out").routeId("test").to("mock:out");
        }
    }
}

 

If you're using Arquillian as testing framework, you need to activate the alternative in your deployment method, e.g.:

 

Code Block
languagejava
@RunWith(Arquillian.class)
public class CamelCdiTest {

    @Deployment
    public static Archive deployment() {
        return ShrinkWrap.create(JavaArchive.class)
        // Camel CDI
        .addPackage(CdiCamelExtension.class.getPackage())
        // Test classes
        .addPackage(Application.class.getPackage())
        // Bean archive deployment descriptor
        .addAsManifestResource(
            new StringAsset(
                Descriptors.create(BeansDescriptor.class)
                    .getOrCreateAlternatives()
                        .stereotype(MockAlternative.class.getName()).up()
                    .exportAsString()),
                "beans.xml");
    }

    //...
}

Camel context customisation

...