Java – how to create acceptance tests for asynchronous microservices
If I have a microservice, it should create users. However, because user creation is very complex, it uses queues. Users are actually created by consumers. The endpoint only accepts requests and returns normal or failed
How to create an acceptance test for this acceptance criteria: given: when do users want to register: request the API to create a user, and then: create a user and set up the host environment on the new user_ id
To do this, I have to wait for the environment to actually set up, which takes 30 seconds If I implement sleep in my test, how can I test it correctly without failing best practices when I click anti pattern wait and see?
Solution
Perhaps the most appropriate is to return the response immediately, let's say "setup process start" (using setup process ID), and then use another API method, which will "get setup status" (for this setup process ID) – and then continue when "setup is complete"
Because, similarly, nothing stays in 30 seconds, whether in test or production - and a progress bar can be displayed to users, indicating the current status so that they can estimate how long it will take - without the impression that something is stuck or ineffective
A can hardly be tested asynchronously, and the setting process itself will not be asynchronous; And long running tasks without any status indicators are almost unacceptable - because it just looks valid and knows what's going on in the background, but doesn't know that
This is an indicator whenever the test encounters an anti - pattern, and the solution may be suboptimal