Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking endpoints in camel-quarkus tests #1981

Open
rschlegel-cc opened this issue Nov 2, 2020 · 14 comments
Open

Mocking endpoints in camel-quarkus tests #1981

rschlegel-cc opened this issue Nov 2, 2020 · 14 comments
Assignees

Comments

@rschlegel-cc
Copy link

camel-quarkus seems to be lacking a way for dealing with mocks for external endpoints in tests: we have Camel routes and use them in Quarkus. For testing, @QuarkusTest works really well and provides us with a CamelContext and everything else we need.

Things get tricky though when we want to mock external endpoints within existing routes, e.g. a call to an external HTTP service. This can be done using AdviceWithRouteBuilder and mockEndpointsAndSkip or more elaborate interceptors, but it modifies the CamelContext. Therefore tests are no longer independent and may fail or succeed depending on the execution order.

Using CamelTestSupport fixes this, as the CamelContext is recreated between tests but using a different CamelContext than the one created by QuarkusTest seems like a bad idea. It also seems to cause issues for certain routes, as the context might not contain everything that is needed, so I would assume that mixing CamelTestSupport and @QuarkusTest should be avoided.

To get a better idea of how this could be done I also looked at the Spring integration and saw that there are annotations that help with endpoint mocking and dealing with dirty contexts. Does something comparable (or plans for it) exist for camel-quarkus? If not, are there examples/best practices for dealing with scenarios like this? Thanks!

@jamesnetherton
Copy link
Contributor

You can always stub out HTTP services with mock responses or use a library like WireMock. For other non-HTTP stuff testcontainers is pretty useful. That's the approach that this project has mostly used thus far.

The problem with replicating the CamelTestSupport stuff, is that it's not simple to have all of the functionality working in native mode.

@rschlegel-cc
Copy link
Author

Thanks for the quick response, this helps quite a bit. And just to be absolutely sure that I did not misunderstand anything: CamelTestSupport should not be used together with camel-quarkus, right?

@jamesnetherton
Copy link
Contributor

CamelTestSupport should not be used together with camel-quarkus, right?

Correct, it should not be used (IMO). There's no usage of it in this project, so we can't give any guarantees that it works properly. And like I mentioned, it wont work in native mode.

Maybe that'll change in the future, but for now I'd avoid it.

aldettinger added a commit to aldettinger/camel-quarkus that referenced this issue Dec 16, 2020
aldettinger added a commit to aldettinger/camel-quarkus that referenced this issue Dec 16, 2020
@tstuber
Copy link
Contributor

tstuber commented Mar 4, 2021

I agree with @rschlegel-cc , it would be very helpful if there would be some more guidance for how to mock endpoints.
We face many situations where we would like to build unit tests routes while mocking the actual endpoints.

@ppalaga
Copy link
Contributor

ppalaga commented May 26, 2021

I filed #2679 to improve the testing page in our user guide.

@neoxu999
Copy link

Hi @jamesnetherton , in most cases, we only need CamelTestSupport in JVM testing, mocking routes.
For native, we can use integration test.
Can you please provide some advice? How to move camel-test to camel-quarkus-test?

@aldettinger
Copy link
Contributor

Hi @neoxu999, I think we are all very busy with other subjects those days but if you are able to invest time for a long effort to explore this path on your own, then here are few directions I could point.

Some users reported that they were able to @Inject a camel route in a @QuarkusTest. This apporach can't be the solution in my opinion and it had some limitations, for instance logging. But it could be a way to start playing with this subject.

Camel Quarkus is about mixing the best of Camel and Quarkus, so for tests I would naively say we would need a @CamelQuarkusTest junit 5 annotation that would somehow assemble the functionalities from @QuarkusTest and CamelTestSupport. Maybe, the first step would be to read/train with JUnit5 annotations, quarkus/cdi lifecycle, see how @QuarkusTest and CamelTestSupport have been implemented.

And on the long run, thinking about an approach that would allow @CamelQuarkusTest to be executed in native mode also would be killer feature. We would maybe need a way to serialize every test related objects from test process to the quarkus application under test process...

@neoxu999
Copy link

neoxu999 commented Oct 13, 2021

Hi @aldettinger , thanks for your very detailed guides, including short and long plan. I will try to add @CamelQuarkusTest and make the JVM mode worked first.

@neoxu999
Copy link

Hi @aldettinger, I checked the new extension guide.
"Let others know that you work on the given extension by either creating a new issue or asking to assign an existing one to you."
https://camel.apache.org/camel-quarkus/latest/contributor-guide/create-new-extension.html

May I get your permission to assign this task to me?

@aldettinger
Copy link
Contributor

The root of this ticket is about being able to mock an endpoint. I guess experimenting with @CamelQuarkusTest that allow to mock an endpoint would be a good first experiment in scope of this ticket. We can refine later on if needed. Let me assign the ticket then.

@ppalaga
Copy link
Contributor

ppalaga commented Oct 14, 2021

Thanks the feedback and for taking the initiative, @neoxu999! As @aldettinger pointed out we are busy with other tasks and assisting with your contribution is all we can offer at this time.

@neoxu999
Copy link

Thanks for your kind words @ppalaga! I will make it worked.

@zhfeng
Copy link
Contributor

zhfeng commented Dec 14, 2021

A simple hack way could be

@QuarkusTest
public class TransactedTest extends CamelTestSupport {
    @Inject
    CamelContext context;

    @Override
    protected CamelContext createCamelContext() throws Exception {
        return this.context;
    }
}

you can check it out on camel-test-demo.

@aldettinger
Copy link
Contributor

aldettinger commented Dec 14, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants