You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@camel.apache.org by "Michel Erard (Jira)" <ji...@apache.org> on 2020/09/07 11:50:00 UTC
[jira] [Comment Edited] (CAMEL-15486) Mocking of Camel Endpoints
breaks because adding 3rd party dependency with spring boot
[ https://issues.apache.org/jira/browse/CAMEL-15486?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17191657#comment-17191657 ]
Michel Erard edited comment on CAMEL-15486 at 9/7/20, 11:49 AM:
----------------------------------------------------------------
So, found out what the problem is.
The Wiremock dependency has the class {{WireMockTestExecutionListener}} that does load the application context in the {{TestExecutionListener#beforeTestClass}} method that is executed earlier than the method {{CamelSpringBootExecutionListener#prepareTestInstance()}}. Means when this code is executed the camel context is already started:
{code:java}
// we are customizing the Camel context with
// CamelAnnotationsHandler so we do not want to start it
// automatically, which would happen when SpringCamelContext
// is added to Spring ApplicationContext, so we set the flag
// not to start it just yet
SpringCamelContext.setNoStart(true);
System.setProperty("skipStartingCamelContext", "true");
{code}
I see two solutions:
# Implement \{{beforeTestClass }}as well and move this code there
# Check if the context is already started and stop it in that case.
was (Author: erard22):
So, found out what the problem is.
The Wiremock dependency has the class {{WireMockTestExecutionListener}} that does load the application context in the {{TestExecutionListener#beforeTestClass}} method that is executed earlier than the method {{CamelSpringBootExecutionListener#prepareTestInstance()}}. Means when this code is executed the camel context is already started:
{code:java}
// we are customizing the Camel context with
// CamelAnnotationsHandler so we do not want to start it
// automatically, which would happen when SpringCamelContext
// is added to Spring ApplicationContext, so we set the flag
// not to start it just yet
SpringCamelContext.setNoStart(true);
System.setProperty("skipStartingCamelContext", "true");
{code}
I see two solutions:
# Imlement {{beforeTestClass }}as well and move this code there
# Check if the context is already started and stop it in that case.
> Mocking of Camel Endpoints breaks because adding 3rd party dependency with spring boot
> --------------------------------------------------------------------------------------
>
> Key: CAMEL-15486
> URL: https://issues.apache.org/jira/browse/CAMEL-15486
> Project: Camel
> Issue Type: Bug
> Components: camel-test
> Affects Versions: 3.4.3
> Reporter: Michel Erard
> Priority: Minor
>
> As soon you add the dependency
> {code:java}
> <dependency>
> <groupId>org.springframework.cloud</groupId>
> <artifactId>spring-cloud-contract-wiremock</artifactId>
> <version>2.2.3.RELEASE</version>
> <scope>test</scope>
> </dependency>{code}
> to your project, the mocking of the endpoints breaks.
> I've created a simple example to reproduce the issue:
> [https://github.com/erard22/spring-boot-camel-bug-demo]
>
> {code:java}
> @Component
> public class SimpleCamelRoute extends RouteBuilder {
> @Override
> public void configure() {
> from("direct:processMessage")
> .to("file:output");
> }
> }
> {code}
> {code:java}
> @CamelSpringBootTest
> @SpringBootTest(classes = DemoApplication.class)
> @ContextConfiguration
> @DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
> @MockEndpoints("file:*")
> class SimpleCamelRouteTest {
> @Autowired
> private CamelContext camelContext;
> @Produce("direct:processMessage")
> private ProducerTemplate producer;
> @EndpointInject("mock://file:output")
> private MockEndpoint mockCamel;
> @Test
> void processMessage_successful() throws Exception {
> mockCamel.expectedBodiesReceived("foo");
> producer.sendBodyAndHeaders("foo", Collections.emptyMap());
> mockCamel.assertIsSatisfied();
> }
> }
> {code}
> {code:java}
> java.lang.AssertionError: mock://file:output Received message count. Expected: <1> but was: <0>
> Expected :<1>
> Actual :<0>
> {code}
>
--
This message was sent by Atlassian Jira
(v8.3.4#803005)