0

I've checked the Camel In Action 2 Chapter-9 examples, and looked at this previous question and this user-group thread, but still stuck...

I am using Spring-boot and Camel 2.18.x I am trying to combine the two examples from Camel sample code, into one: to mockEndpoint and also replaceFrom

Working scenario:

  1. I create a test route direct-->seda
  2. I use Advicewith, and mock all endpoints
  3. My test works fine

Working scenario:

  1. I changed the destination, to have direct-->jms
  2. I get an exception at the last, and see JMS failing to create a session.

Expectation: I assume that JMS would be replaced by the mock, and the logs seem to indicate so. Not sure why the JMSProducer is being invoked anyway. Is this expected behavior?

Sample Route:

    from("direct:start")
            .id("testroute")
            .log("${body}")
            //.to("seda:finish")   //This works okay
            .to("jms:XYZ_Q")
     ;

Unit-test class:

@RunWith(CamelSpringBootRunner.class)
@MockEndpoints
@UseAdviceWith
@SpringBootTest(classes = {UnitTestApplication.class, SampleTest.class})
public class SampleTest {

    @Autowired
    private CamelContext camelContext;

    @Autowired
    private ProducerTemplate producerTemplate;

    @Test
    public void test01() throws Exception {
        RouteDefinition route = camelContext.getRouteDefinition("testroute");
        AdviceWithRouteBuilder adviceWithRB = new AdviceWithRouteBuilder() {
            @Override
            public void configure() throws Exception {
                replaceFromWith("direct:renamed");
            }
        };

        route.adviceWith(camelContext, adviceWithRB);
        camelContext.start();

        producerTemplate.sendBody("direct:renamed", "  8888888820130601");
    }
}

I expected that the JMS component would not attempt to do anything, but would be replaced by the mock. Is this an incorrect understanding?

Community
  • 1
  • 1
Darius X.
  • 2,886
  • 4
  • 24
  • 51

1 Answers1

2

I assume that JMS would be replaced by the mock

  1. Nope, it won't be replaced, because replaceFromWith(..) replaces the route's input (from(..)) with a new endpoint URI. It should work in the following case (used your example):

    from("jms:XYZ_Q")
        .id("testroute")
        .log("${body}");
        //.to("seda:finish")   //This works okay
        //.to("jms:XYZ_Q")
    
  2. If you would like to mock the .to("jms:XYZ_Q") part (from your question) then you can use the weaveByIdToString(), weaveById(), etc functions. More info: http://camel.apache.org/advicewith.html#AdviceWith-UsingAdviceWithRouteBuilder

mgyongyosi
  • 2,557
  • 2
  • 13
  • 20