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

Better support for Spring Boot's @DataMongoTest #551

Open
rfelgent opened this issue Jun 24, 2022 · 8 comments
Open

Better support for Spring Boot's @DataMongoTest #551

rfelgent opened this issue Jun 24, 2022 · 8 comments
Assignees

Comments

@rfelgent
Copy link

rfelgent commented Jun 24, 2022

Hello,

mongock gets triggered in a test without any problem when performing a fully fledged spring boot test e.g.

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Testcontainers   // spinning up mongdb container by testcontainers library
class ApplicationIT {
}

Mongock is failing when doing a mongo slice test

@DataMongoTest
@Import(MongockConfig.class)   // a dedicated config class for mongock stuff like `@EnableMongock` and other beans that are only required for mongock migration stuff
@Testcontainers // spinning up mongdb container by testcontainers library
class RepositoryIT {
}

I found a way to fix this by manually adding a driver bean to the spring context

@TestConfiguration
  static class Fixture {

    @Bean
    public SpringDataMongoV3Driver driver(@Autowired MongoTemplate mongoTemplate) {
      return SpringDataMongoV3Driver.withDefaultLock(mongoTemplate);
    }

  }

So I do observe that Mongock's autoconfiguration for "driver" beans is failing within a @DataMongoTest.
I am using SB 2.7 and SB 2.6.X and only the Mongock version is 5.1.0

Best regards

@rfelgent rfelgent changed the title Better test-integration of mongock v5 in Spring Boots slice test @DataMongoTest Better support for Spring Boots slice test @DataMongoTest Jun 24, 2022
@rfelgent rfelgent changed the title Better support for Spring Boots slice test @DataMongoTest Better support for Spring Boot's @DataMongoTest Jun 24, 2022
@rfelgent
Copy link
Author

rfelgent commented Sep 23, 2022

problem still exists with Mongock 5.1.6 and Spring Boot 2.6.5 (Spring-Data-MongoDB v3.3.3)

@dieppa
Copy link
Member

dieppa commented Sep 28, 2022

Hello @rfelgent ,

we are in the middle of something important right now. Is this blocking you?
Thanks

@Saljack
Copy link

Saljack commented Apr 5, 2023

We have to add this configuration with @DataMongoTest:

@DataMongoTest
@EnableMongock
@Import({SpringDataMongoV4Context.class}) // it can be also SpringDataMongoV3Context for Spring Boot 2

Mainly find the SpringDataMongoV4Context was a little tricky. It is an autoconfiguration class. For enabling this autoconfiguration also for tests you need to add this file META-INF/spring/org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo.imports
With this content:

io.mongock.driver.mongodb.springdata.v4.config.SpringDataMongoV4Context

When I find some time I will create a MR.

@rfelgent
Copy link
Author

rfelgent commented Apr 5, 2023

Hi @Saljack ,

exactly; either use V3Context.class oder V4Context.class depending on the Sprint Boot version you use.

Are you planning to create meta annatotation ? My 2 cents:

  • Is there one meta annotation supporting all Spring Boot versions or a dedicated annotation for each Spring Boot versions ?
  • furthermore, the mongock team has a different approach and a dedicated chapter for doing integration tests. I am not sure, what way to go or if multiple approaches/integrations should be supported by mongock itself

@Saljack
Copy link

Saljack commented Apr 6, 2023

Hmm, I realized it is not a good idea to just add META-INF/spring/org.springframework.boot.test.autoconfigure.data.mongo.AutoConfigureDataMongo.imports because it is then in production code. So I think using @Import({SpringDataMongoV4Context.class}) with @EnableMongock is cleaner solution. Otherwise, there would be a mongock-spring-test with this configuration but I do not see it as an advantage to add another test dependency. We have our custom testing library on our project so we can add this autoconfiguration there.

@dieppa dieppa closed this as completed Jun 27, 2023
@rfelgent
Copy link
Author

rfelgent commented Jul 2, 2023

Hi @dieppa ,

I see that you have closed the issue. Is there now an out-of-the-box solution provided by mongock library ?

@rfelgent
Copy link
Author

rfelgent commented Feb 2, 2024

hello again,

sorry to bother you guys.
This issue is still relevant for me in my current project. I am using spring boot 3.2.2 and mongock 5.4.0.
As you closed the issue, I would like to know what is the solution provided by mongock library?

My current work-around for tweaking the DataMongoTest slice-test is to use a custom meta-annotation as follow:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@DataMongoTest
@AutoConfigureJson
@Import({MongockConfig.class, DataMongoTestFixture.TestConfig.class})
@Testcontainers
@DirtiesContext
public @interface DataMongoTestFixture {

  @TestConfiguration
  // https://github.com/mongock/mongock/issues/551
  class TestConfig {

    @Bean
    public SpringDataMongoV4Driver driver(@Autowired MongoTemplate mongoTemplate) {
      return SpringDataMongoV4Driver.withDefaultLock(mongoTemplate);
    }
  }
}

I have no problems with mongock initialization during test execution when using plain SpringBootTest

@dieppa dieppa reopened this Feb 2, 2024
@dieppa
Copy link
Member

dieppa commented Feb 2, 2024

It has been re-opened, we'll take a look to see th best option to approach this and will give you an answer with the estimation soon

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

4 participants