You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Burkard Stephan <St...@visana.ch> on 2017/06/07 10:27:20 UTC

Mocks receive no messages in Camel-SpringBoot test

Hi 

I built Camel route tests in a Spring Boot project. My injected mock endpoints do not receive any messages. I also tried to get the mock through Camel context, but also without success.

The log output says that the mock endpoints are created and the mocked endpoint receives messages. Have I missed something or is this a known problem? If the latter, how can I bring my mocks to work?

I have attached a very simple Maven test project to show the problem. 

SpringBoot 1.4.2.RELEASE
Camel 2.17.3

Thanks 
Stephan

AW: Mocks receive no messages in Camel-SpringBoot test

Posted by Burkard Stephan <St...@visana.ch>.
Hi Owain

Thanks for your answer. The Problem was in fact the TestRunner. I used "CamelSpringJUnit4ClassRunner" in my example project. If I simply change this to "CamelSpringBootJUnit4ClassRunner" (notice the "Boot" in it), the test in my example project works fine. 

@MockEndpoints and @EndpointInject are working like a charm, no need to advice the routes to just get Mocks for your endpoints.

Regards
Stephan
 


-----Ursprüngliche Nachricht-----
Von: Owain McGuire [mailto:owain@integration.technology] 
Gesendet: Mittwoch, 7. Juni 2017 13:45
An: users@camel.apache.org
Betreff: Re: Mocks receive no messages in Camel-SpringBoot test

Stephan,

You appear not to be mocking the activemq:queue:output endpoint before you get the mock endpoint i the test.  If you temporarily change the route uri to mock:activemq:queue:output then the test works.  I am not sure how you would do it using injection.  I use advicewith on the route to mock URIs before the test.  Here is a previous response https://stackoverflow.com/questions/29581561/in-an-apache-camel-application-how-can-unit-tests-inject-mock-endpoints-in-plac <https://stackoverflow.com/questions/29581561/in-an-apache-camel-application-how-can-unit-tests-inject-mock-endpoints-in-plac> to the same issue.

Here is another code snippet

@RunWith(CamelSpringBootRunner.class)
@MockEndpoints
@UseAdviceWith
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class AssignOrderReferenceTests {


  public static final String ROUTE_ID = “myroute";
  public static final String MOCK_ENRICH = "mock:put";
  public static final String MOCK_OUT = "mock:out";
  
  @Autowired
  private ProducerTemplate template;

  @Autowired
  private CamelContext camelContext;

  @Test
  public void formatReferenceFromCounter() throws Exception {


    camelContext.getRouteDefinition(ROUTE_ID).adviceWith(camelContext, new AdviceWithRouteBuilder() {
      @Override
      public void configure() throws Exception {
        replaceFromWith("direct:in");
        weaveById(“id-of-endpoint").replace().inOut(MOCK_ENRICH);  // you can use enpoint patterns.  Have a look at the adviceWith docs. 
        weaveAddLast().to(MOCK_OUT);
      }
    });

    MockEndpoint mockEnrich = camelContext.getEndpoint(MOCK_ENRICH, MockEndpoint.class);
    MockEndpoint mockOut = camelContext.getEndpoint(MOCK_OUT, MockEndpoint.class);
      mockEnrich.whenAnyExchangeReceived(new Processor() {
      @Override
      public void process(Exchange exchange) throws Exception {
        exchange.getOut().setBody(COUNTER);
      }
    });

    mockEnrich.expectedMessageCount(1);
    mockOut.expectedMessageCount(1);

    mockOut.expectedBodyReceived().constant(Prefix + (COUNTER));

    camelContext.start();

    template.sendBody("direct:in", 1);

    mockOut.assertIsSatisfied();


The AdviceWith is very powerful. 

HTH.

O.
> On 7 Jun 2017, at 11:27, Burkard Stephan <St...@visana.ch> wrote:
> 
> Hi 
> 
> I built Camel route tests in a Spring Boot project. My injected mock endpoints do not receive any messages. I also tried to get the mock through Camel context, but also without success.
> 
> The log output says that the mock endpoints are created and the mocked endpoint receives messages. Have I missed something or is this a known problem? If the latter, how can I bring my mocks to work?
> 
> I have attached a very simple Maven test project to show the problem. 
> 
> SpringBoot 1.4.2.RELEASE
> Camel 2.17.3
> 
> Thanks 
> Stephan
> <camelbootmocks.zip>


Re: Mocks receive no messages in Camel-SpringBoot test

Posted by Owain McGuire <ow...@integration.technology>.
Stephan,

You appear not to be mocking the activemq:queue:output endpoint before you get the mock endpoint i the test.  If you temporarily change the route uri to mock:activemq:queue:output then the test works.  I am not sure how you would do it using injection.  I use advicewith on the route to mock URIs before the test.  Here is a previous response https://stackoverflow.com/questions/29581561/in-an-apache-camel-application-how-can-unit-tests-inject-mock-endpoints-in-plac <https://stackoverflow.com/questions/29581561/in-an-apache-camel-application-how-can-unit-tests-inject-mock-endpoints-in-plac> to the same issue.

Here is another code snippet

@RunWith(CamelSpringBootRunner.class)
@MockEndpoints
@UseAdviceWith
@SpringBootTest(classes = MyApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class AssignOrderReferenceTests {


  public static final String ROUTE_ID = “myroute";
  public static final String MOCK_ENRICH = "mock:put";
  public static final String MOCK_OUT = "mock:out";
  
  @Autowired
  private ProducerTemplate template;

  @Autowired
  private CamelContext camelContext;

  @Test
  public void formatReferenceFromCounter() throws Exception {


    camelContext.getRouteDefinition(ROUTE_ID).adviceWith(camelContext, new AdviceWithRouteBuilder() {
      @Override
      public void configure() throws Exception {
        replaceFromWith("direct:in");
        weaveById(“id-of-endpoint").replace().inOut(MOCK_ENRICH);  // you can use enpoint patterns.  Have a look at the adviceWith docs. 
        weaveAddLast().to(MOCK_OUT);
      }
    });

    MockEndpoint mockEnrich = camelContext.getEndpoint(MOCK_ENRICH, MockEndpoint.class);
    MockEndpoint mockOut = camelContext.getEndpoint(MOCK_OUT, MockEndpoint.class);
      mockEnrich.whenAnyExchangeReceived(new Processor() {
      @Override
      public void process(Exchange exchange) throws Exception {
        exchange.getOut().setBody(COUNTER);
      }
    });

    mockEnrich.expectedMessageCount(1);
    mockOut.expectedMessageCount(1);

    mockOut.expectedBodyReceived().constant(Prefix + (COUNTER));

    camelContext.start();

    template.sendBody("direct:in", 1);

    mockOut.assertIsSatisfied();


The AdviceWith is very powerful. 

HTH.

O.
> On 7 Jun 2017, at 11:27, Burkard Stephan <St...@visana.ch> wrote:
> 
> Hi 
> 
> I built Camel route tests in a Spring Boot project. My injected mock endpoints do not receive any messages. I also tried to get the mock through Camel context, but also without success.
> 
> The log output says that the mock endpoints are created and the mocked endpoint receives messages. Have I missed something or is this a known problem? If the latter, how can I bring my mocks to work?
> 
> I have attached a very simple Maven test project to show the problem. 
> 
> SpringBoot 1.4.2.RELEASE
> Camel 2.17.3
> 
> Thanks 
> Stephan
> <camelbootmocks.zip>