naXa stands with Ukraine
naXa stands with Ukraine

Reputation: 37946

NoClassDefFoundError org/springframework/jdbc/core/ConnectionCallback in tests

I'm stuck, I get this error even after I excluded anything related to JDBC from Gradle dependencies:

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/jdbc/core/ConnectionCallback
    at java.lang.Class.getDeclaredMethods0(Native Method)
    ... (skipped)
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.core.ConnectionCallback
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    ... (skipped)

The stack trace contains only internal classes of Gradle, Java, and JUnit. I don't understand which class requires the JDBC driver at runtime.

I run gradle integrationTest where integrationTest is my custom Gradle task. I tried to create a Minimal, Reproducible Example and for this problem. I created a similar but smaller gradle project where I used this task and some key dependencies and it worked well (I couldn't reproduce this error so I can't share a small code with you, sorry). And I think it means that my custom task integrationTest works fine, the problem is somewhere in transitive dependencies of the main project.

I tried gradle dependencies | grep jdbc and excluded 1 library that has a mention of jdbc in name. After this, the problem persists.

I tried to add a new dependency to the test scope of the main project:

integrationTestImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc'

After this, the problem is gone. But I don't like this solution because it looks like a workaround, and I want to find a root cause of this problem. The tested module of the main project does not need to connect to relational databases (only Mongo) to do its work, so it should not require JDBC driver at runtime. Right?

Please help me to debug this.

Upvotes: 4

Views: 10106

Answers (1)

kasptom
kasptom

Reputation: 2458

I had the same error when I've migrated my project from MySQL to MongoDB. I had one test class annotated with

import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

// other imports ...

@DataJpaTest
@RunWith(SpringRunner.class)
class SomeIntegrationTestIT {
// ...
}

when I've replaced the annotation (and the import) with

import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;

// other imports ...

@DataMongoTest
@RunWith(SpringRunner.class)
class SomeIntegrationTestIT {
// ...
}

the error disappeared.

Upvotes: 8

Related Questions