6

Re: https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4

I tried the @DataJpaTest to test my repository but my application is using Springfox, so with Springfox @EnableSwagger2 the test execution will fail with the following error:

java.lang.IllegalStateException: Failed to load ApplicationContext
...    
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'documentationPluginsBootstrapper' defined
...
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.requestmappinginfohandlermapping>' available

What can be done to address this? Otherwise, it is impossible to do slice testing using @DataJpaTest.

Code:

Application class:
@SpringBootApplication
@EnableSwagger2
public class CurrencyApplication {
  @Bean
  public Module datatypeHibernateModule() {
    return new Hibernate5Module();
  }

  public static void main(String[] args) {
    SpringApplication.run(CurrencyApplication.class, args);
  }

  @Bean
  public Docket currencyApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.any())
        .paths(PathSelectors.any())
        .build()
        .pathMapping("/")
        ;
  }
}

Test class:

@RunWith(SpringRunner.class)
@DataJpaTest
public class ExchangeRateRepoTest {

  @Test
  public void doNothing() {
  }
}
pumpump
  • 361
  • 2
  • 15
  • I am not sure what you are trying with . Please share some code. Dont try to load all the spring context while doing slice test. this can be done by removing @SpringBootTest – Barath Jan 16 '17 at 10:34

1 Answers1

15

Move @EnableSwagger out of the SpringBootApplication

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Configuration
@EnableSwagger2
class AdditionalConfig {

}
Dapeng
  • 1,704
  • 13
  • 25
  • 1
    Also good for @DataMongoTest. Note that if you have other @Enable* annotations they best also be moved to the Config class. – Muhd Jun 26 '17 at 04:54
  • Saved the day! Thanks a lot – small_ticket Oct 29 '17 at 13:01
  • I was having problems to test with @DataJpaTest, because of this. When trying to run the test I was receiving a `No ServletContext set` error. A quite vague message. Your answer nailed it. I am putting this here to help others when Googling how to solve the same issue... it costed me 2 hours to solve this. Thank you a lot – Paullus Nava Sep 27 '19 at 09:44