2

I've a simple camel route and want to test it, but instead of the MockEndpoint the real Endpoint is called:

import org.apache.camel.builder.RouteBuilder;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;
    
@Component
@Slf4j
public class ProcessingRoute extends RouteBuilder {

    @Override
    public void configure() {

        from("direct:processKafkaMessage")
                .to("file:output");

        }
}

My test:

import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
import org.apache.camel.test.spring.junit5.MockEndpoints;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;

@ActiveProfiles("test")
@CamelSpringBootTest
@SpringBootTest()
@ContextConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@MockEndpoints("file:output")
class ProcessingRouteIT {

    @Autowired
    private CamelContext camelContext;

    @Produce("direct:processKafkaMessage")
    private ProducerTemplate mockKafkaProducer;

    @EndpointInject("mock:file:output")
    private MockEndpoint mockCamel;

    @Test
    void processMessage_successful() throws Exception {
        mockCamel.expectedBodiesReceived("foo");
        mockKafkaProducer.sendBodyAndHeaders("foo", Collections.emptyMap());
        mockCamel.assertIsSatisfied();
    }

}

It's perfeclty fine if I use this code in a simple example project with nothing more inside. But when I put it in my application that has some more routes and other stuff inside, it does not mock the endpoint and write to the file instead.

13:58:09.579  INFO [main] o.a.c.t.s.j.CamelAnnotationsHandler      - Enabling auto mocking of endpoints matching pattern [file:output] on CamelContext with name [camelContext].
13:58:57.010  INFO [main] .i.e.InterceptSendToMockEndpointStrategy - Adviced endpoint [file://output] with mock endpoint [mock:file:output]

14:14:21.969  INFO [main] c.t.s.j.CamelSpringBootExecutionListener - CamelSpringBootExecutionListener before: class ProcessingRouteIT.processMessage_successful
14:14:21.971  INFO [main] c.t.s.j.CamelSpringBootExecutionListener - Initialized CamelSpringBootExecutionListener now ready to start CamelContext
14:14:26.953  INFO [main] o.a.camel.component.mock.MockEndpoint    - Asserting: mock://file:output is satisfied

I'm using in both applications the same versions of camel (3.3.0) and spring boot (2.3.1.RELEASE). Any Idea? I don't get it...

java.lang.AssertionError: mock://file:output Received message count. Expected: <1> but was: <0>
Expected :<1>
Actual   :<0>
boskop
  • 609
  • 5
  • 23
  • 1
    You have provided an example, which you yourself say works in isolation, and only stops working when you put it inside your application. This points to the fact that the error is not present in the code you have provided, and only triggered by some other thing in your application. That makes it almost impossible to guess what the problem is, since we basically have to guess how you might have set up your application. – kalusn Aug 13 '20 at 13:40
  • A way forward for you is to gradually remove stuff from your application until it starts working again. Then you can better pinpoint which part of your application is interfering with the mocks in your test. – kalusn Aug 13 '20 at 13:54
  • Had the same idea. I blindly removed some dependencies and it worked. Now I try to figure out which solved it in detail. – boskop Aug 13 '20 at 14:35
  • Ok, found the dependency that makes the test fail: org.springframework.cloud spring-cloud-contract-wiremock 2.2.3.RELEASE test – boskop Aug 13 '20 at 14:58

0 Answers0