spring-boot: @WebMvcTest fails with @EnableFeignClients
Hi
Problem
In my app I’ve got a service which makes a rest call to an external API using Feign. I want to mock this external call in my @WebMvcTest by using the @MockBean annotation, but I get the following exception.
20:40:32.236 [main] WARN o.s.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@314c8b4a] to process 'before' execution of test method [public void example.ExampleApplicationTest.shouldReturnDescription() throws java.lang.Exception] for test instance [example.ExampleApplicationTest@26d820eb]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'example.GitHub': FactoryBean threw exception on object creation; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.feign.FeignContext] is defined
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1600)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:68)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:55)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.beforeTestMethod(ResetMocksTestExecutionListener.java:45)
at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.cloud.netflix.feign.FeignContext] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:346)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1088)
at org.springframework.cloud.netflix.feign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:159)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 30 common frames omitted
Repro steps
I’ve created an example project to reproduce this error. The pull request: https://github.com/spring-projects/spring-boot-issues/pull/60
Temporary workaround
I was able to bypass this problem by adding to the test the feign client interface annotated with @MockBean.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (5 by maintainers)
Links to this issue
Commits related to this issue
- using getSingleton method in ResetMocksListener resolves gh-7270 — committed to alebar/spring-boot by alebar 8 years ago
- 微调配置注解的位置,将@EnableFeignClients挪到FeignConfig。因为将@EnableFeignClients放在@SpringBootApplication上会导致使用@WebMvcTest的测试失败 https://github.com/spring-projects/spring-boot/issues/7270 — committed to iuwjh/gulimallEx by iuwjh 4 years ago
- 微调配置注解的位置,将@EnableFeignClients挪到FeignConfig。因为将@EnableFeignClients放在@SpringBootApplication上会导致使用@WebMvcTest的测试失败 https://github.com/spring-projects/spring-boot/issues/7270 — committed to iuwjh/gulimallEx by iuwjh 4 years ago
Okay, I have just spent over 4 hours on this issue… Seems like there is not anything out there yet about the solution to this stuff…
Here is what I did to solve this: 1. Add this annotation to your test class:
Try it, if it does not work, you might need the
@EnableFeignClients annotationon your main program configWhy this issue is closed?
Adding
@ImportAutoconfiguration({RibbonAutoConfiguration.class, FeignRibbonClientAutoConfiguration.class, FeignAutoConfiguration.class})is ugly workaround.Nothing in this thread helped Using
@AutoConfigureMockMvc+@SpringBootTestinstead@WebMvcTestfix that issue for me.Thanks for explanation, so valid workaround is to simply create class
and removing
@EnableFeignClientsfrom SpringBootApplication class.I solved this problem just by @EnableAutoConfiguration