'Spring testing onProprtiesSet with embedded mongodb

We have some logic that runs on application start using InitializingBean.onPropertiesSet that depends on the contents of the mongo database and we want to test it. Specifically, we trigger some code the saves a state in the db and then the causes the app to restart, and after that the state in the db is asserted in onPropertiesSet.

I tried to do the following:

@SpringBootTest(...some properties)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
public class TestClass {

  @Test
  public void dummyTest() {
    // trigger app restart which also saves the state in the db
  }

  @Test(dependsOnMethods = { "dummyTest" })
  public void realTest() {
    // test that the code executed in onPropertiesSet() is correct
  }
}

DirtiesContext is used to trigger the onProprtiesSet.

This logic runs fine except that DirtiesContext clears the database so then there is nothing to assert on restart.

I'd like to hear how this can be solved. Other methods of testing such scenarios are also welcome (I've seen people use the @sql annotation to inject data before the test starts, but I haven't been able to find something similar for mongodb), although testing the entire scenario is preferred.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source