Hi guys/gals,
We have a tiny issue with integration tests that I would like to share so that others having the same issue can solve it in an efficient and official way.
Our setup includes an Axon Server SE with development mode enabled and several Spring Boot applications, some of which contain projection models in JPA repositories. We use Postman to run integration tests. The issue is (as you would suspect) preparing data before each test and cleaning up afterwards (or beforehand).
A general structure of our integration test suite looks like:
- Erase event store
- Erase projections in app1
- Erase projections in app2
- Erase projections in app3
- Load “test” data for app1
- Load “test” data for app2
- Load “test” data for app3
- Run test(s) which send requests and check responses across one or more cooperating apps
As you can see, just preparing an environment to actually run a test requires seven steps while only having three apps. Our naive strategy was to shutdown all apps including Axon Server, delete all data files and databases and then start everything back up. With 10+ apps it takes like forever to setup an environment for a single test suite. And we have a lot of test suites with many more in the making as the applications grow. This gives us quite a lot of headaches and cost a lot of processing and our time.
As I understand it, the official way is to have a @ResetHandler
method with something as simple as repository.deleteAll()
in all of my event handling projectors. On top of that, it is also necessary to do a shutdown-resetEvents-start
sequence on all TrackingEventProcessors
, as noted for example here by @Corrado_Musumeci and elsewhere.
All of this seems easy to do with a simple application. However, having a multitude of applications and each having another multitude of projection repositories quickly leads to a lot of duplicated code.
Since I could not find anything reusable in the official guide nor on discuss, I have come up with my own reusable solution which requires only a few tweaks in each application and works kind of out-of-the-box. The Axon Server already provides an endpoint to erase events, so I followed the example of providing a similar DELETE
endpoint to
- Shutdown, reset events and start again all
TrackingEventProcessor
s - Erase all data in all JPA repositories
All of the above is accomplished just by adding an @Import(ResetConfiguration.class)
annotation to a correct place in each application.
Here is the source code and a tiny guide:
Would you consider such approach as sensible or completely wrong and recommend something different?
Thanks for any feedback and/or ideas,
David