springfox: @EnableSwagger2 breaking my unit tests

Hey guys - we are trying to use this at the NFL, and we are running into this exception when adding @EnableSwagger2 annotation. It’s odd because it works on some projects. We’re not quite able to pin point the problem. If I remove the annotation and just keep @Configuration, it builds fine, but when I try to access the swagger UI at host:port/swagger-ui.html from a browser, it shows a pop up: Unable to infer base url. This is common when using dynamic servlet registration… etc.

Can you help please?

java.lang.IllegalStateException at TokenUtilTest.java:43
        Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at TokenUtilTest.java:43
            Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException at TokenUtilTest.java:43
                Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException at TokenUtilTest.java:43

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 42 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I had the similar issue when I enabled Swagger2 Config. All my unit tests are failing with the below error straktrace.

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189) at org.springframework.test.context.web.ServletTestExecutionListener.pr

Here is my code which is causing my Junits to fail.

@RunWith(SpringRunner.class) @SpringBootTest @WebMvcTest(MyRESTServiceImpl.class)

I fixed this issue by adding @WebAppConfiguration @RunWith(SpringRunner.class) @SpringBootTest @WebMvcTest(MyRESTServiceImpl.class) @WebAppConfiguration

Hope it helps! with you upgrading your spring-boot version and junit version.

I have the same issue and not only in integration test but in the regular application too. Exception it reported randomly, it happens approximately once per 5 server starts.

Once I’ve downgraded from 2.7.0 to 2.6.0 the issue is gone.