spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

@JsonTest does not load ApplicaitonContext correctly.

Open hantsy opened this issue 10 years ago • 12 comments

I am trying to use@JsonTest to the @JsonComponent, but it seems there are some ApplicationContext loading problem existed.

The sample is hosted on my github, there is a 1.4-upgrade branch to taste Spring Boot 1.4.0M2.

https://github.com/hantsy/angularjs-springmvc-sample-boot/.

The test codes:

LocalDateTimeJsonComponentTest

If I used @JsonTest instead of @SpringBootTest, it will report the ApplicationSecurity constructor injection failure. The following is the logging details.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.hantsylabs.restexample.springmvc.Application$ApplicationSecurity': Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [com.hantsylabs.restexample.springmvc.repository.UserRepository] found for dependency [com.hantsylabs.restexample.springmvc.repository.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hantsylabs.restexample.springmvc.repository.UserRepository] found for dependency [com.hantsylabs.restexample.springmvc.repository.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:841) ~[spring-context-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:104) [spring-boot-test-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49) [spring-boot-test-autoconfigure-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) [surefire-booter-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) [surefire-booter-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [surefire-booter-2.18.1.jar:2.18.1]
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hantsylabs.restexample.springmvc.repository.UserRepository] found for dependency [com.hantsylabs.restexample.springmvc.repository.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    ... 43 common frames omitted

2016-04-23 21:31:09.879 ERROR 5944 --- [main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener@6580cfdd] to prepare test instance [com.hantsylabs.restexample.springmvc.test.LocalDateTimeJsonComponentTest@31c88ec8]

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) ~[spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) ~[spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49) ~[spring-boot-test-autoconfigure-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) ~[spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit-4.12.jar:4.12]
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit-4.12.jar:4.12]
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) [spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128) [surefire-junit4-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) [surefire-booter-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) [surefire-booter-2.18.1.jar:2.18.1]
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [surefire-booter-2.18.1.jar:2.18.1]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.hantsylabs.restexample.springmvc.Application$ApplicationSecurity': Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [com.hantsylabs.restexample.springmvc.repository.UserRepository] found for dependency [com.hantsylabs.restexample.springmvc.repository.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hantsylabs.restexample.springmvc.repository.UserRepository] found for dependency [com.hantsylabs.restexample.springmvc.repository.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:841) ~[spring-context-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) ~[spring-boot-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:104) ~[spring-boot-test-1.4.0.M2.jar:1.4.0.M2]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) ~[spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ~[spring-test-4.3.0.RC1.jar:4.3.0.RC1]
    ... 25 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hantsylabs.restexample.springmvc.repository.UserRepository] found for dependency [com.hantsylabs.restexample.springmvc.repository.UserRepository]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1398) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.0.RC1.jar:4.3.0.RC1]
    ... 43 common frames omitted

hantsy avatar Apr 23 '16 13:04 hantsy

@JsonTest is not a replacement for @SpringBootTest; it is a way to easily test json serialization. Your application class is expecting a bunch of beans to be present (for instance JPA to be present with this UserRepository). They won't. You should move all that code to separate @Configuration classes.

snicoll avatar Apr 23 '16 14:04 snicoll

@snicoll

UserRepository is a Spring Data JPA interface. I have to disable JPA in projects to use @JsonTest?

I found @DataJpaTest, @WebMvcTest have the same problems, they do not load ApplicationContext as the @SpringBootTest, I really do not know the design purpose of the @JsonTest, @DataJpaTest, @WebMvcTest if they can not be used freely.

hantsy avatar Apr 23 '16 14:04 hantsy

This is explained in the documentation. It's test slicing (only starting a part of the actual application to have tests that are isolated on a single concept). DataJpaTest for instance only starts JPA (and does not scan your controllers, etc. If that's not what you want, just keep @SpringBootTest and use the new Autoconfigure annotations. In your case that should be AutoConfigureJsonTesters.

The new infra also auto-detects your @SpringBootApplication. If you have a bunch of requirements there, that's not going to work.

snicoll avatar Apr 23 '16 15:04 snicoll

I wanted to test @JsonTest and I believe I have the same issue. I added a new test class that is only annotated with @JsonTest. When I run it, it tries to load my DatabaseUserDetailsService as well, but it seems the entityManagerFactory is not present:

No bean named 'entityManagerFactory' is defined

Like @hantsy, I am wondering how you can use this in a real world application.

If I understand things correctly, it would be more clear to have an annotation on the JacksonTester field instead of the class or Spring Boot should only create the ObjectMapper and the @JsonComponent beans (Of course, those beans might be dependent on other beans).

wimdeblauwe avatar May 19 '16 15:05 wimdeblauwe

@wimdeblauwe Do you have a sample that you can share? The JsonExcludeFilter class tries to filter out non-relevant beans so I'm wondering why DatabaseUserDetailsService is still included.

I'm not following what you mean about the "annotation on JacktonTest" can you clarify? It is possible to use that class directly with your own ObjectMapper. Use JacksonTester.initFields (see the class-level Javadoc).

philwebb avatar May 21 '16 09:05 philwebb

@philwebb I have added a sample project at https://github.com/wimdeblauwe/spring-boot-issue-5776/tree/master. See the README there for some more info.

wimdeblauwe avatar May 23 '16 07:05 wimdeblauwe

@wimdeblauwe The problem you're seeing is because you have directly declared @EnableJpaRepositories, @EnableJpaAuditing and @ComponentScan annotations. You can fix @ComponentScan by adding an additional filter:

@ComponentScan(excludeFilters = { @Filter(pattern = ".*TestConfiguration", type = FilterType.REGEX),
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class) })

The other ones are a little more tricky to fix and I'm not sure what we should do. You might be better off using a full @SpringBoot test in combination with @AutoConfigureJsonTesters, or using JacksonTester.initFields.

philwebb avatar May 23 '16 16:05 philwebb

The @ComponentScan is only there to exclude any inner classes that are Spring configurations just for a unit test. Without that, the application might pick beans from those tests contexts. What would be the recommended way to handle that?

I have @EnableJpaRepositories and @EnableJpaAuding because I am using Spring Data JPA. Are you saying I don't need to declare those?

wimdeblauwe avatar May 23 '16 16:05 wimdeblauwe

@wimdeblauwe Boot 1.4 adds a new @TestComponent and @TestConfiguration annotations which can be used on components that Spring Boot's default scanning should ignore (the JavaDoc has more details).

You can safely drop @EnableJpaRepositories and it will be auto-configured. I'm not sure about @EnableJpaAuding, that one might be a problem.

philwebb avatar May 23 '16 16:05 philwebb

Great news on @TestComponent and @TestConfiguration. Must have missed it in the release notes. In case anybody else wonders, this is the link to the docs about that: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-excluding-config

wimdeblauwe avatar May 23 '16 16:05 wimdeblauwe

@wimdeblauwe Were you able to solve the issue? In my case having a @Configuration class (which is not the Application configuration class but a different one in a sub-package) annotated with @EnableJpaAuditing causes the @Service components to be initialized by the @WebMvcTest auto configuration.

raspacorp avatar Dec 08 '16 02:12 raspacorp

@raspacorp I don't think your issue is the same as mine. You might want to create a post on stackoverflow to get help with that.

wimdeblauwe avatar Dec 08 '16 19:12 wimdeblauwe