Archer
Archer

Reputation: 5147

Camel Mock endpoint does not receive any message

Very simple route:

import org.apache.camel.builder.RouteBuilder

class TestRoutes extends RouteBuilder {    
  @Override
  void configure(){
    from("direct:foo").to("mock:bar")
  }
}

very simple test case:

package mocktest

import static org.junit.Assert.*
import org.junit.*

import org.apache.camel.CamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.test.junit4.CamelTestSupport

class TestRouteTests extends CamelTestSupport {

  def CamelContext camelContext
  def ProducerTemplate producerTemplate

  @Test
  void testSomething() {

    getMockEndpoint('mock:bar').expectedMessageCount(1)
    producerTemplate.sendBody('direct:foo', "Hello World")

    assertMockEndpointsSatisfied()
  }
}

Getting this:

| Failure:  testSomething(mocktest.TestRouteTests)
|  java.lang.AssertionError: mock://bar Received message count. Expected: <1> but was: <0>

Tried with camel:2.12.1, camel:2.11.1 Broke my head today trying to solve. Complete test grails project which contains only this issue is here: https://github.com/gutsal-arsen/mocktest

Could anyone please help me to solve?

UPDATE 1

  @Test
  void testSimpleRoute() {
    def mockEndpoint
    mockEndpoint = camelContext.getEndpoint('mock:bar')
    //mockEndpoint = getMockEndpoint('mock:bar')                                                                                                                                                                                               

    mockEndpoint.expectedMessageCount(1)

    producerTemplate.sendBody('direct:foo', 'Hello World')

    mockEndpoint.assertIsSatisfied()
  }

This one does not:

   @Test
  void testSimpleRoute() {
    def mockEndpoint
    //mockEndpoint = camelContext.getEndpoint('mock:bar')
    mockEndpoint = getMockEndpoint('mock:bar')                                                                                                                                                                                               

    mockEndpoint.expectedMessageCount(1)

    producerTemplate.sendBody('direct:foo', 'Hello World')

    mockEndpoint.assertIsSatisfied()
}

So the problem is inside getMockEndpoint() method.

Upvotes: 4

Views: 7555

Answers (2)

Archer
Archer

Reputation: 5147

To solve this issue we should override CamelTestSupport#createCamelContext method, since default implementation create separate camel context, which does not contain our routes (hence, mock endpoints are fake).

Like this:

   protected CamelContext createCamelContext() throws Exception {                                                                                                                         
    return camelContext;
  }

The full source now looks like:

package mocktest

import static org.junit.Assert.*
import org.junit.*

import org.apache.camel.CamelContext
import org.apache.camel.impl.DefaultCamelContext
import org.apache.camel.ProducerTemplate
import org.apache.camel.test.junit4.CamelTestSupport
import org.apache.camel.builder.RouteBuilder

import org.apache.camel.processor.interceptor.Tracer
import org.apache.camel.processor.interceptor.DefaultTraceFormatter
import org.apache.camel.LoggingLevel

class TestRouteTests extends CamelTestSupport {

  def CamelContext camelContext
  def ProducerTemplate producerTemplate

  protected CamelContext createCamelContext() throws Exception {                                                                                                                         
    return camelContext;
  }

  @Test
  void testSomething() {
    def mockEndpoint
    //mockEndpoint = camelContext.getEndpoint('mock:bar') // this works                                                                                                                  
    mockEndpoint = getMockEndpoint('mock:bar') // this works now also                                                                                                                          

    mockEndpoint.expectedMessageCount(1)
    producerTemplate.sendBody('direct:foo', "Hello World")

    assertMockEndpointsSatisfied()                                                                                             
  }
}

Upvotes: 2

Ben ODay
Ben ODay

Reputation: 21015

you never defined the route to send messages to the mock:bar endpoint...add something like this

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:foo").to("mock:bar");
        }
    };
}

Upvotes: 0

Related Questions