How to mock AMQP consumers in Camel testing?

Let's say I have the following route:

from(rabbitMQUri)
    .to(myCustomerProcessor)
    .choice()
        .when(shouldGotoA)
            .to(fizz)
        .when(shouldGotoB)
            .to(buzz)
        .otherwise()
            .to(foo);

Assume that myCustomProcessorconfigures shouldGotoAand shouldGotoBin accordance with the message consumed via RabbitMQ.

I would like to use unit test 3 scripts:

  • The message "fizz" is reported and shouldGotoAset to true, which the first executes when(...).
  • The message "buzz" is reported and shouldGotoBset to true, which the second executes when(...).
  • The message "foo" is reported and executed otherwise().

: / RabbitMQ, , , , , RabbitMQ? - "mock message".

!

+4
3

.

Camel ProducerTemplate :

<camel:camelContext id="camelContext">
   <camel:template id="producerTemplate" />
</camel:camelContext>

, , , , , , .

Camel. Spring, :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/spring/spring-test-camel.xml" })
public class MyTest {

    @Autowired
    private ProducerTemplate producerTemplate;

    @Autowired
    private CamelContext camelContext;

RabbitMQ/ActiveMQ/JMS seda . , JMS seda.

camelContext.removeComponent("jms");
camelContext.addComponent("jms", this.camelContext.getComponent("seda"));
camelContext.addRoutes(this.documentBatchRouting);

, URI JMS, seda. URI , , , URI.

, :

producerTemplate.sendBody("jms:MyQueue", 2);

, .

:

  • , JMS

  • , , Camel .

+4

, (AMQP RabbitMQ) .

Camel - junit .

, - , , , , : AMQPRouteTest.java RabbitMQConsumerIntTest.java

"" - "" uri . , RouteBuilder :

   private String fromURI = "amqp:/..";

   public void setFromURI(String fromURI){
     this.fromURI = fromURI;
   }

   public void configure(){
     from(fromURI).whatever();
   }

"seda: foobar" fromURI unit test. seda . , AMQP/RabbitMQ, .

+3

( , ) - . Guice, , ( , , )

" ". , ( "" ).

@Provides
@Named("FileEndpoint")
private Endpoint fromFileEndpoint() {
    FileEndpoint fileEndpoint = getContext().getEndpoint("file:" + somFolder, FileEndpoint.class);
    fileEndpoint.setMove(".done");
    fileEndpoint.setRecursive(true);
    fileEndpoint.setDoneFileName(FtpRoutes.DONE_FILE_NAME);
    ...
    return fileEndpoint;
}

RouteBuilder :

@Inject
private MyRoutes(@Named("FileEndpoint") final Endpoint fileEndpoint) {
    this.fileEndpoint = fileEndpoint;
}
@Override
public void configure() throws Exception {
    from(fileEndpoint)....
}

, , FileEndpoint, "direct: something". "Jukito" , Guice Mockito. :

@RunWith(JukitoRunner.class)
public class OcsFtpTest extends CamelTestSupport {

    public static class TestModule extends JukitoModule {
        @Override
        protected void configureTest() {
            bind(CamelContext.class).to(DefaultCamelContext.class).in(TestSingleton.class);
        }
        @Provides
        @Named("FileEndpoint")
        private Endpoint testEndpoint() {
            DirectEndpoint fileEndpoint = getContext().getEndpoint("direct:a", DirectEndpoint.class);
        return fileEndpoint;
        }
    }
    @Inject
    private MyRoutes testObject;

   @Test
   ....
}

"testObject" . , , / Apis, ( !).

+1

All Articles