0

I have a Spring-boot application with a custom filter, FilterRegistrationBean.

When creating this filter, during bean instantiation, i try to search all annotations of a specific type in the classpath with some code.

ClassPathScanningCandidateComponentProvider provider = createComponentScanner();
provider.findCandidateComponents("").stream()...

The call to findCandidateComponents throws a NoClassDefFoundError.

The application works during normal operation, but fails in unittest. My test class like:

@RunWith(SpringRunner.class)
@SpringBootTest
public class YadaYadaApplicationTests
{
    @Test
    public void contextLoads()
    {
    }
}

If I comment out "@SpringBootTest" i can run normally.

I guess it is something as the classpath is not the same when running unit test as when i run the application stand alone.

These are my dependencies:

implementation('org.springframework.boot:spring-boot-starter-web')
testImplementation('org.springframework.boot:spring-boot-starter-test')

What can I do to resolve this?

user1578293
  • 215
  • 2
  • 10
  • May [this post](https://stackoverflow.com/questions/33950862/maven-noclassdeffounderror-org-springframework-test-context-junit4-springjunit) helps you? – juanlumn Nov 14 '18 at 15:55

1 Answers1

0

Found a solution.

Apparently the 'org.springframework.boot:spring-boot-starter-test' requires some more dependencies which are not transitively inherited.

Guess the reason might be I manually scan the classpath with ClassPathScanningCandidateComponentProvider, which will load dependet classes, not on the classpath.

Adding the following solves my problem:

build.gradle:

testImplementation('org.springframework.boot:spring-boot-starter-jdbc')
testImplementation('org.apache.derby:derby')

application.properties:

spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
user1578293
  • 215
  • 2
  • 10