1

Why does this test fails with "java.lang.AssertionError: mock://destino Received message count. Expected: <12> but was: <0>" ? I'm just trying to test if Camel can really reorder messages.

Imports:

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.CamelContext;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.guice.CamelModuleWithRouteTypes;
import org.jukito.JukitoModule;
import org.jukito.JukitoRunner;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;

The test:

@RunWith(JukitoRunner.class) public class ResequenceTest {

    @Inject protected CamelContext context;
    @Produce protected ProducerTemplate template;

    public static class Module extends JukitoModule {
        @SuppressWarnings("unchecked") protected void configureTest() {
            install(new CamelModuleWithRouteTypes(OrderingTestRouteBuilder.class));
        }
    }

    @Test public void testDozenMsgsOrderByIntegerBody() throws Exception {
        // fail();
            Integer[] input = new Integer[] {12, 11, 10, 9, 8, 7, 6, 5, 1, 2, 3, 4};
            List<Integer> expectedOutput = new ArrayList<Integer>();
        for (int i=1; i<=12; i++) {expectedOutput.add(i);};
        for (Integer i : input) {template.sendBody("mock:receptor", i);};
        MockEndpoint resultEndpoint = context.getEndpoint("mock:destino", MockEndpoint.class);
        resultEndpoint.expectedBodiesReceived(expectedOutput);
        resultEndpoint.assertIsSatisfied();
    }

}

The route:

class OrderingTestRouteBuilder extends RouteBuilder {
        @Override public void configure() throws Exception {
        from("direct:start")
            .to("mock:receptor")
            .resequence(body(Integer.class))
            .to("mock:destino");
    }
}

Thanks, Rodolfo

Rodolfo
  • 261
  • 4
  • 10

2 Answers2

5

You should setup the expectations on the mock before you send messages into Camel. The steps is

  1. setup expectations on the mocks
  2. send messages
  3. assert
Claus Ibsen
  • 56,060
  • 7
  • 50
  • 65
1

It's passing now. I changed test to:

@Test public void testDozenMsgsOrderByIntegerBody() throws Exception {
    // fail();
    List<Integer> input = Arrays.asList(new Integer[] {12, 11, 10, 9, 8, 7, 6, 5, 1, 2, 3, 4});
    List<Integer> expectedOutput = new ArrayList<Integer>();
    for (int i=1; i<=12; i++) {expectedOutput.add(i);};
    MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedBodiesReceived(expectedOutput);
    for (Integer i : input) {template.sendBody("direct:start", i);};
    resultEndpoint.assertIsSatisfied();
}

and the route to:

class OrderingTestRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
    from("direct:start")
       .resequence(body(Integer.class))
       .to("mock:result");
    }
}
Rodolfo
  • 261
  • 4
  • 10