mongock

Mongock execute range of change-units in integration test


Question: How can I execute certain range of change-units in integration tests ?

Example of the problem: I have three change-units: X,Y,Z (with order set to: 1, 2, 3). I have integration tests T1 for X, Y and T2 for Z. How can I execute only migrations X and Y in test T1 (and "skip" Z) ?

My solution: I see only two ways.

Assume that each test would have its own runner (like we have in MongockIntegrationTestBase#mongockBeforeEach):

  1. Each migration file (or a group of files) should be put in one package. And the runner would have addMigrationScanPackage specified (to narrow down test to only those migrations that are found in that package). In this case we would have package P1 with migrations X and Y and package P2 with Z, so it would be:

    runnerBuilder.addMigrationScanPackage("P1");
    return runnerBuilder.buildRunner();
    
  2. Instead of the package, we would use ChangeUnit#systemVersion and execute runner with properties: SystemVersionable#setStartSystemVersion and SystemVersionable#setEndSystemVersion. In this case we would have systemVersion in X,Y,Z set to 1,2,3 and before running the test T1 we would have:

    runnerBuilder.setStartSystemVersion("1");
    runnerBuilder.setEndSystemVersion("2");
    return runnerBuilder.buildRunner();
    

Both solutions are not satisfactory for me.

  1. The first one looks weird to create a separate package per migration file
  2. The second one is slightly better, but:

Is there any other way I can solve that problem ?


Solution

  • That's a weird case, as you either want to run the entire migration or performing an unit test only in one change unit, which you can do by just running the method annotated with @Execution.

    However, a workaround to achieve that is something similar to your first solution. Create your own runner, but instead of passing the package, you can pass the list of classes directly with addMigrationClasses(List<Class<?>> classes) or addMigrationClass(Class<?> clazz)