database-rider / database-rider

Database testing made easy!

Home Page:https://database-rider.github.io/database-rider

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SpringBootDBUnitTest fails with @BeforeAll & @DataSet annotated static method

niels1voo opened this issue · comments

@rmpestano I didn't see a test in the codebase that tested @BeforeAll, so I added a hook to SpringBootDBUnitTest, which causes the following exception.

com.github.database.rider.core.exception.DataBaseSeedingException: Could not initialize dataset: emptyUsers.yml

at com.github.database.rider.core.dataset.DataSetExecutorImpl.createDataSet(DataSetExecutorImpl.java:140)
at com.github.database.rider.junit5.DBUnitExtension.executeDataSetForCallback(DBUnitExtension.java:188)
at com.github.database.rider.junit5.DBUnitExtension.beforeAll(DBUnitExtension.java:147)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$invokeBeforeAllCallbacks$8(ClassBasedTestDescriptor.java:368)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeBeforeAllCallbacks(ClassBasedTestDescriptor.java:368)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:192)
at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.before(ClassBasedTestDescriptor.java:78)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:136)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:143)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:129)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:127)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:126)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:84)
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:108)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67)
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:96)
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:75)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

Caused by: java.lang.RuntimeException: JDBC connection url cannot be empty
at com.github.database.rider.core.dataset.DataSetExecutorImpl.getConnectionFromConfig(DataSetExecutorImpl.java:482)
at com.github.database.rider.core.dataset.DataSetExecutorImpl.access$000(DataSetExecutorImpl.java:46)
at com.github.database.rider.core.dataset.DataSetExecutorImpl$1.getConnection(DataSetExecutorImpl.java:475)
at com.github.database.rider.core.connection.RiderDataSource.getConnection(RiderDataSource.java:88)
at com.github.database.rider.core.connection.RiderDataSource.init(RiderDataSource.java:94)
at com.github.database.rider.core.connection.RiderDataSource.(RiderDataSource.java:53)
at com.github.database.rider.core.dataset.DataSetExecutorImpl.getRiderDataSource(DataSetExecutorImpl.java:912)
at com.github.database.rider.core.dataset.DataSetExecutorImpl.performSequenceFiltering(DataSetExecutorImpl.java:320)
at com.github.database.rider.core.dataset.DataSetExecutorImpl.createDataSet(DataSetExecutorImpl.java:127)
... 41 more

The problem seems to be related to Spring.isEnabled returns false when springStore.get(extensionContext.getTestClass().get()) returns null. In other words, it appears the lifecycle hook is executed before the context is populated.

Hi @niels1voo, you mean the @DataSet is being ignored because of the exception? I assume everything fails

Interesting, let me know if you find any fix and I'll have a look on my side as well. Thanks for the feedback!

Yes, it fails right away, before it has a chance to create the dataset.

I've attached a few logs. In passes-spring-is-enabled.log I set a break point at Spring.isEnabled line 24. In passes-complete.log I let it run through to completion. You can see that the spring context is loaded before database rider checks whether spring is enabled.
In fails-with-before-all.log you can see that db rider kicks in before spring context is loaded.

fails-with-before-all.log
passes-complete.log
passes-spring-is-enabled.log

The problem stems from the order of declaration of the extensions in the test.

https://junit.org/junit5/docs/current/user-guide/#extensions-registration-declarative

Extensions registered declaratively via @ExtendWith at the class level, method level, or parameter level will be executed in the order in which they are declared in the source code.

I switched the order to place SpringBootTest first, which eliminated the exception and allowed the test to pass. I think this can be closed, though it would be helpful to add a warning to the documentation.

Hey @niels1voo, thank you for digging into this and also for the PR!