@@ -112,21 +112,25 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
112112 }
113113 }
114114
115- protected void optimizedDbReset (TestContext testContext , FlywayTest annotation ) throws Exception {
115+ protected synchronized void optimizedDbReset (TestContext testContext , FlywayTest annotation ) throws Exception {
116116 if (annotation != null && annotation .invokeCleanDB () && annotation .invokeMigrateDB () && !annotation .invokeBaselineDB ()) {
117117
118118 ApplicationContext applicationContext = testContext .getApplicationContext ();
119-
120119 Flyway flywayBean = ReflectionTestUtils .invokeMethod (this , "getBean" , applicationContext , Flyway .class , annotation .flywayName ());
121- FlywayDataSourceContext dataSourceContext = getDataSourceContext (applicationContext , flywayBean );
122120
123- if (dataSourceContext != null && flywayBean != null ) {
124- prepareDataSourceContext (dataSourceContext , flywayBean , annotation );
121+ if (flywayBean != null ) {
122+ FlywayDataSourceContext dataSourceContext = getDataSourceContext (applicationContext , flywayBean );
123+
124+ if (dataSourceContext != null ) {
125125
126- FlywayTest adjustedAnnotation = copyAnnotation ( annotation , false , false , true );
127- ReflectionTestUtils . invokeMethod ( this , "dbResetWithAnnotation" , testContext , adjustedAnnotation );
126+ dataSourceContext . getTarget (); // wait for completion of running flyway migration
127+ prepareDataSourceContext ( dataSourceContext , flywayBean , annotation );
128128
129- return ;
129+ FlywayTest adjustedAnnotation = copyAnnotation (annotation , false , false , true );
130+ ReflectionTestUtils .invokeMethod (this , "dbResetWithAnnotation" , testContext , adjustedAnnotation );
131+
132+ return ;
133+ }
130134 }
131135 }
132136
0 commit comments