Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NPE upon closing connections in DbUnitRunner after tests have run #140

Open
Hellago opened this issue Jul 8, 2019 · 2 comments
Open

NPE upon closing connections in DbUnitRunner after tests have run #140

Hellago opened this issue Jul 8, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@Hellago
Copy link

Hellago commented Jul 8, 2019

Hello,
First, thanks for your contribution to the community.

I get the following error :

08/07/2019 16:59:33 - WARN : Caught exception while invoking 'afterTestMethod' callback on TestExecutionListener [com.github.springtestdbunit.DbUnitTestExecutionListener@224f520c] for test method [public void MyTest.myTestFunction()] and test instance [MyTest@28040a43] | (TestContextManager.java:548)

  • java.lang.NullPointerException
  • com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:113)
  • com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:185)
  • org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:443)
  • org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:94)
  • org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
  • org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:298)
  • org.junit.internal.runners.statements.FailOnTimeout$CallableStatement.call(FailOnTimeout.java:292)
  • at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
  • at java.base/java.lang.Thread.run(Thread.java:834)

Adding a test
if (testContext.getConnections() != null)
solves the problem, but I guess the cause is somewhere else.

I'm using :

  • Java 11
  • JUnit 4.12
  • Spring Batch 4.1.2.RELEASE
  • Spring Core 5.1.8.RELEASE
  • DbUnit 2.5.4

Here is my code :
dbunit-context.xml :

	<bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean" lazy-init="true">
		<property name="qualifiedTableNames" value="false" />
		<property name="skipOracleRecyclebinTables" value="true" />
	</bean>
	<bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean.DatabaseDataSourceConnectionFactoryBean" lazy-init="true">
		<property name="dataSource" ref="myDataSource" />
		<property name="databaseConfig" ref="dbUnitDatabaseConfig" />
	</bean>

Test class Java :

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
        DbUnitTestExecutionListener.class })
@DbUnitConfiguration(databaseConnection = { "myConnection" }, dataSetLoader = MyCustomDataSetLoader.class)
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class MyTest {
	// ...
}

If you can't find the cause, can you at least turn the error into a warning ?

Thank you in advance.

@ppodgorsek
Copy link
Owner

ppodgorsek commented Jul 21, 2019

Sorry for the delay.

The NullPointerException is caused by no connection being available in the test context.
From what I see above, you are not using the right bean name:

  • dbUnitDatabaseConnection is the bean name
  • myConnection is the connection defined on the test class

Could you use the same name in both and retry please?

In the meantime, I will adjust the code to avoid the NPE and log a proper message instead.

@ppodgorsek ppodgorsek changed the title NPE on DbUnitRunner#afterTestMethod line 103 NPE upon closing connections in DbUnitRunner after tests have run Jul 21, 2019
@ppodgorsek ppodgorsek added the bug Something isn't working label Jul 21, 2019
@Hellago
Copy link
Author

Hellago commented Jul 23, 2019

My bean "dbUnitDatabaseConnection" is a factory (badly named I agree), not a connection.
I had another piece of code specifying the bean "myConnection" :

	<bean id="myConnection" class="org.dbunit.database.DatabaseConnection" lazy-init="true">
		<constructor-arg name="connection" ref="realConnection" />
	</bean>

Messing up with the name gives the following exception at start :

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'myConnection2' available

Messing up with the type of the bean gives that error :

java.lang.IllegalArgumentException: Object of class [java.sql.Connection] must be an instance of interface org.dbunit.database.IDatabaseConnection

So I think the cause is somewhere else.
Maybe because I handle connections a bit "manually" ?

final QueryDataSet qds = new QueryDataSet(myConnection);
qds.addTable(tableName, sqlQuery);
MyCustomDataSetLoader.save(qds, saveFile);

ppodgorsek added a commit that referenced this issue Aug 2, 2019
ppodgorsek added a commit that referenced this issue Aug 2, 2019
Issue #140 - NPE upon closing connections in DbUnitRunner after tests have run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants