0
votes

I'm unable to solve this. I have a spring boot application.

Main class annotated with

@SpringBootApplication.

Test class annotated with:

@RunWith(SpringRunner.class)
@SpringBootTest

Everything is fine until i add a filter-configurer class in the application:

@Configuration
public class YadaYadaFilterConfigurer
{
    @Bean
    public FilterRegistrationBean<YadaYadaFilter> createYadaYadaFilter() 
    {...}

    ...
}

Boom!

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: 
...
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:...org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException

My dependencies:

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

Some clearification. Stripped so that I have only this test-class:

@RunWith(SpringRunner.class)
@SpringBootTest
public class JsonvalidationApplicationTests {

    @Test
    public void contextLoads() {
    }
}

If comment out the @SpringBootTest then everything works. Seems like this annotation introduces a need for more dependencies?

Some more stacktrace:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.servlet.FilterRegistrationBean]: Factory method 'createyadayadaFilter' threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583) ... 67 more Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/66122872/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-test-autoconfigure/2.0.6.RELEASE/b0eb15474e795850dfa59b01ee6a83d7a39e3645/spring-boot-test-autoconfigure-2.0.6.RELEASE.jar!/org/springframework/boot/test/autoconfigure/jdbc/JdbcTest.class]; nested exception is java.lang.NoClassDefFoundError: org/springframework/dao/DataAccessException

1
You should also include spring-boot-starter-parent dependency. - GauravRai1512
If those are your only dependencies you are missing stuff, judging from the Exception at least. Apparently you are using data Access somewhere or accidentily import an @JdbcTest annotation in your test class. But I suspect that you only added a snippet of the stacktrace and that the original stacktrace is langer. - M. Deinum
@GauravRai1512 Adding spring-boot-starter-parent, still yields the same problem. - user1578293
@M.Deinum If I uncomment "@SpringBootTest" everything works. I stripped everything so I only have one emty test class that does nothing. I have no Data access in the application. Seemls like "@SpringBootTest" is including the data access dependencies. I will update my post a bit. - user1578293
Could be the auto config for testing that has a hard dependency on it. You might want to create a small test case and create an issue in the spring Boot issue tracker. - M. Deinum

1 Answers

0
votes

Sorry, after some more investigation i found that the problem is in my FilterRegistrationBean.

the exeption is thrown here.

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

I will answer this thread and create a new.