0

I am facing issue with Integration Test on one of Controller Test in SpringBoot 1.4. Below snippets will show a clear idea of project structure:

class ExchangeControllerIT :

    public class ExchangeControllerIT extends AbstractSpringControllerIT {

      // class under test
      @Autowired
      private ExchangeController exchangeController;

      @Autowired
      private OAuth2RestTemplate restTemplate;

      @Test
      public void shouldSuccessWhileExchange() throws Exception {
        // given       
        controllerHas(mockExchangeServiceReturningStringResponse());
        // then      
        getMockMvc().perform(get(Uris.Exchange).accept(MediaType.TEXT_HTML)
                .content(asString(ExchangeControllerIT.class, "")))
                .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.parseMediaType(MediaType.TEXT_HTML + ";charset=UTF-8")));        
       }

       private void controllerHas(ExchangeService exchangeService) {
           Reflections.setField(exchangeController, "exchangeService", exchangeService);
       }

       private static ExchangeService mockExchangeServiceReturningStringResponse() {
        return new HandShakeService();           
       }
}

Abstract Class below:

    public abstract class AbstractSpringControllerIT extends AbstractSpringIT{

       protected MockMvc getMockMvc() {
           return webAppContextSetup(getApplicationContext()).build();
       }
    }

AbstractSpringIT class:

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)
    public abstract class AbstractSpringIT {

       @Autowired(required=true)
       private GenericWebApplicationContext ctx;
       protected final GenericWebApplicationContext getApplicationContext() {
           return ctx;
       }
   }

I am new to SpringBoot and Tests, help me find out cause and probable solution

StackTrace for above mentioned error:

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.getOrFindConfigurationClasses(SpringBootTestContextBootstrapper.java:173)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.processMergedContextConfiguration(SpringBootTestContextBootstrapper.java:133)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:409)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildDefaultMergedContextConfiguration(AbstractTestContextBootstrapper.java:323)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:277)
at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:112)
at org.springframework.boot.test.context.SpringBootTestContextBootstrapper.buildTestContext(SpringBootTestContextBootstrapper.java:78)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:105)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTestContextManager(SpringJUnit4ClassRunner.java:152)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.<init>(SpringJUnit4ClassRunner.java:143)
at org.springframework.test.context.junit4.SpringRunner.<init>(SpringRunner.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:96)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Om Bhakre
  • 1
  • 4
  • Would you be able to include the complete stack trace? – megalucio Mar 06 '17 at 11:19
  • @megalucio i added the stackTrace.. can you look if it gives any hint – Om Bhakre Mar 06 '17 at 11:38
  • What packages are your tests and your main application class in? – Andy Wilkinson Mar 06 '17 at 12:50
  • @AndyWilkinson i have main and other classes under **com.org.abc** and have **resources/com.org.abc** and same path for test classes under test directory. I have **application.properties and applicationContext.xml** under **resources/com.org.abc** both in main and test directory. **Appcontext and TestAppContext(which imports applicationContext.xml) under com.org.abc/subfolder** in main and test directory respectively – Om Bhakre Mar 07 '17 at 06:22

1 Answers1

-1

Looks like even though you are using the @SpringBootTest annotation you are not including its classes parameter where you will need to specify the classes that you will need to be loaded in your context in order for your test to run successfully.

Check what classes you need and include them there:

@SpringBootTest(classes=...)

Also, although may be not the best solution, if you do not mind to reload the whole spring context for your test you could just use the @SpringBootConfiguration annotation in your test class instead of @SpringBootTest

Nicomedes E.
  • 1,326
  • 5
  • 18
  • 27
megalucio
  • 5,051
  • 2
  • 22
  • 26
  • thanks megalucio i tried the same on ExchangeControllerIT class, now it gives **nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.** but on which of above class should i add @SpringBootConfiguration annotation – Om Bhakre Mar 06 '17 at 11:43
  • I think it will be in AbstractSpringControllerIT. Also check my answer again as I modified my previous answer based on provided stack trace. – megalucio Mar 06 '17 at 11:53
  • I tried @SpringBootConfiguration on AbstractSpringControllerIT, but it didnt worked – Om Bhakre Mar 06 '17 at 12:14
  • Sorry I meant in AbstractSpringIT in substitution of the other annotations. – megalucio Mar 06 '17 at 12:23
  • I added the @SpringBootTest(classes=) annotation on AbstractSpringIT class. Although i got build success, but with Exception warning **Exception: java.lang.IllegalStateException thrown from the UncaughtExceptionHandler in thread "Thread-5** What does this mean?? – Om Bhakre Mar 07 '17 at 05:42
  • According to [Spring Docs](http://docs.spring.io/spring-boot/docs/current/reference/pdf/spring-boot-reference.pdf#page=160&zoom=auto,-74,715) ,as i am using SpringBootTest(classes=) annotation there is no need to add @SpringBootConfiguration – Om Bhakre Mar 07 '17 at 05:44
  • Yeah that is what I meant by in substitution of in my comment above... I just updated also my initial answer to make it more clear... – megalucio Mar 07 '17 at 07:19