Skip to content

Fix test mongock#1

Open
dieppa wants to merge 4 commits into
pkubowicz:mainfrom
dieppa:fix_test_mongock
Open

Fix test mongock#1
dieppa wants to merge 4 commits into
pkubowicz:mainfrom
dieppa:fix_test_mongock

Conversation

@dieppa
Copy link
Copy Markdown

@dieppa dieppa commented Apr 13, 2022

Hello @pkubowicz .

Aplogies for the delay - I finally got the chance to take a look at your project. I forked it and did a PR with some suggested changes on how it is best practice to write the tests with Mongock.

During the analysis, I found out that you are loading the SpringContext - which already executes the Mongock migration - then you clean the data and execute Mongock again.

This is not the best approach for Mongock given the migration will already run during the loading time of the Spring Boot Application.

I'd suggest to do segregate the test cases:

  • Have a unit test for that can test the changeUnit independently and
  • Have an integration test that can test the entire Mongock process independently from Spring boot
  • Have an integration test the entire Spring boot application with the Mongock migration in it.

This issue mainly covers the last one, testing the entire Spring boot application so that the Mongock runner again is not executed again.

To summarise: run the Spring boot application, with the Mongock process, ensuring some preconditions (in this case some pre-populated data in the database). This can be achieved in different ways, depending on the case, here you can create a InitializingBean like this:

package com.example.mongock;

@TestConfiguration
public class InitializerContext {

    @Bean
    @Order(1)
    public InitializingBean initData(MongoDatabaseFactory mongoDatabaseFactory) {
        return new InitializingBean() {
            @Override
            public void afterPropertiesSet() throws Exception {

                MongoDatabase mongoDatabase = mongoDatabaseFactory.getMongoDatabase();
                mongoDatabase.getCollection("user").deleteMany(new Document());
                mongoDatabase.getCollection("user").insertMany(
                        Stream.of("alice", "bob", "chris")
                                .map(name -> new Document().append("email", name + "@mail.com"))
                                .collect(toList())
                );
                mongoDatabase.getCollection("summary").deleteMany(new Document());
            }
        };
    }
}

and adding @Import(InitializerContext.class):

@SpringBootTest
@Import(InitializerContext.class)
@ContextConfiguration(initializers = MongoInitializer.class)
public abstract class BaseCountingMigrationTest {

Then you can remove the MongockRunner and leave the test as simple as:

    @Test
    public void works() {
        var found = mongoDatabase.getCollection("summary").find();
        assertThat(found).hasSize(1);
        //noinspection ConstantConditions
        assertThat(found.first().getInteger("userCount")).isEqualTo(3);
    }

I hope this clarifies your case :)

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

Successfully merging this pull request may close these issues.

1 participant