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

Annotation for resetting context, threads and containers for integration tests #26818

Open
R4V3NN0 opened this issue Jul 19, 2022 · 23 comments
Open
Labels
area/testing kind/enhancement New feature or request

Comments

@R4V3NN0
Copy link

R4V3NN0 commented Jul 19, 2022

Description

Before I switched to Quarkus, I used Spring and Spring has a feature which I really miss in Quarkus. Dont get me wrong I love working with Quarkus.

But Spring has the @DirtiesContext annotation and its very valuable in integration tests. I know that Quarkus has @TestTransaction but this seems not to work all the time. It worked fine until we integrated our database which also uses transactions and at this point @TestTransaction stopped working. In Spring this was never a problem but in Quarkus we have now the problem that our integration tests somehow depend on each other because @TestTransaction does not reset the context correctly.

Hopefully I explained the problem well, so maybe we could implement @DirtiesContext for Quarkus which restarts the context and restarts all dev-services.

Implementation ideas

Ideally we would implement a @DirtiesContext annotation which restarts the context, kills all active threads - so every thread which was started gets killed and does not effect the next test - and restarts all containers which were started by dev-services or Testcontainers.

@quarkus-bot
Copy link

quarkus-bot bot commented Jul 19, 2022

/cc @geoand

@geoand
Copy link
Contributor

geoand commented Jul 20, 2022

Which kinds of Quarkus test are you talking about specifically?

@R4V3NN0
Copy link
Author

R4V3NN0 commented Jul 20, 2022

Im talking about integration tests, so tests which are annotated with @QuarkusTest and where the whole context is spinned up. Sometimes we even spin up external services for more complete system tests.

@geoand
Copy link
Contributor

geoand commented Jul 20, 2022

You can already do this manually by using a test profile: https://quarkus.io/guides/getting-started-testing#testing_different_profiles

@R4V3NN0
Copy link
Author

R4V3NN0 commented Jul 20, 2022

As far as I have understood this we need to write a profile for each @Quarkustest to restart the context. Does this mean that we have to write 1000 profiles when we have 1000 @QuarkusTest? Or am I getting this wrong?

@geoand
Copy link
Contributor

geoand commented Jul 20, 2022

Indeed you would have to

@R4V3NN0
Copy link
Author

R4V3NN0 commented Jul 20, 2022

I think it would be nice if we could implement an addition to the @QuarkusTest annotation like @QuarkusTest(ResetStrategy.AfterEach) so the context would be restartet after each test. Then you could have AfterEach, AfterClass or None something like in JUnit.
The way with @TestTransaction is nice but it is not really an option in bigger projects. We have like 400 @QuarkusTest and thats to much for adding a Testprofile for each.

@geoand
Copy link
Contributor

geoand commented Jul 20, 2022

Thanks for the feedback. We'll definitely consider it

@mihaipoenaru
Copy link

I would like something like this too. Something like Spring's annotation DirtiesContext that requires a test class to start with a "clean context".

@geoand
Copy link
Contributor

geoand commented Jul 22, 2022

There is no such thing a context restart in Quarkus. If we do something, it will be the exact same thing that we do when switching test profiles

@mihaipoenaru
Copy link

Ah, fair enough. In the meantime, I've already solved my issue. It was the bug that was just solved with 2.10.3 for #26594 . I thought I messed up my tests and I had to "restart the context" in order to clear the mocks properly. Shows how much I know. 🤡

@geoand
Copy link
Contributor

geoand commented Jul 22, 2022

Yeah, that was an oversight on our part

@famod
Copy link
Member

famod commented Jul 23, 2022

Is this primarily about cleaning up data from a previous test?

If you have many tests, fully restarting everything after each test will be no fun in terms of runtime!

@geoand
Copy link
Contributor

geoand commented Jul 26, 2022

If you have many tests, fully restarting everything after each test will be no fun in terms of runtime!

Yeah, it will be very very slow...

@R4V3NN0
Copy link
Author

R4V3NN0 commented Jul 27, 2022

Yes its primarly about cleaning data and it would be very slow if we would do that for every single test but it would still be nice if it could work like in spring. Even if it could be very slow if you use it at all tests. I think it is cleaner and safer than trying to clear old data by myself especially at massive multithreading with multiple async processes.

@deathwaiting
Copy link

This is a bit late reply,
But if it is just reseting test data, spring actually provides the @Sql annotation, which runs a sql script before/after test classes or methods.
We usually depend on this in our spring projects , and no context re-run is needed

I actually made a similar annotation in my quarkus project using CDI bean injection, so, it will be nice addition for integration tests.

@geoand
Copy link
Contributor

geoand commented Nov 11, 2022

I actually made a similar annotation in my quarkus project using CDI bean injection, so, it will be nice addition for integration tests

Can you show what that looks like @deathwaiting?

@deathwaiting
Copy link

deathwaiting commented Nov 12, 2022

I actually made a similar annotation in my quarkus project using CDI bean injection, so, it will be nice addition for integration tests

Can you show what that looks like @deathwaiting?

@geoand

    @Test
    @Sql(executionPhase = BEFORE_TEST_METHOD, scripts ="sql/test_data.sql")
    @Sql(executionPhase = AFTER_TEST_METHOD, scripts ="sql/clear_database.sql")
    public void getEventsList(){
        //test logic
    }

springs annotation targets both classes and methods, in the case of annotating a test class, it will run for each test method inside it

@geoand
Copy link
Contributor

geoand commented Nov 12, 2022

Great, thanks

@HerrDerb
Copy link
Contributor

HerrDerb commented Apr 25, 2023

Is there already something similar? I have the feeling that the my beans in my container are getting messed up with @InjectMock and @InjectSpy and would like to test the behaviour if I could reset the application including all beans before a test

@geoand
Copy link
Contributor

geoand commented Apr 25, 2023

That should not be the case. If they are, we'll need a sample application that shows this problem so we can address it

@GustavoAngeloDS
Copy link

Hi guys, do you plan to work on this?
Are we still not having a viable alternative for this scenario?

@geoand
Copy link
Contributor

geoand commented Jan 30, 2024

It's in the backlog, but it's not high priority

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/testing kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants