title: java spring unit test failure , failed to load ApplicationContext
body: <p>I'm trying to do a unit test in my application to test a a method on service layer.</p>
  
  <p>I have the following classes.</p>
  
  <p><strong>AbstractTest.java</strong></p>
  
  <pre><code>import org.junit.runner.RunWith;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  import com.equadis.webapp.controller.UserController;
  
  //inform spring which class to use when executing unit test
  @RunWith(SpringJUnit4ClassRunner.class)
  
  @SpringBootTest(classes=UserController.class)
  public abstract class AbstractTest {
      protected Logger logger = LoggerFactory.getLogger(this.getClass());
  
  
  }
  </code></pre>
  
  <p>and </p>
  
  <p><strong>UserServiceTest.java that extends AbstractTest</strong></p>
  
  <pre><code>import org.junit.After;
  import org.junit.Assert;
  import org.junit.Before;
  import org.junit.Test;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.equadis.webapp.DAO.UserDAO;
  import com.equadis.webapp.entity.UserVO;
  import com.equadis.webapp.test.AbstractTest;
  
  
  
  @Transactional
  public class UserServiceTest extends AbstractTest {
  
      @Autowired
      private UserService service;
  
  
      @Before
      public void setup(){
          service = new UserService();
      }
  
      @After
      public void tearDown(){
          //clean up after each test method
      }
  
  
      @Test
      public void getListByID() {
          UserVO user = service.getListByID(1);
          System.out.println(user);
          Assert.assertNotNull("failure - expected not null", user);
          Assert.assertEquals("failure - expected id", 1, user.getId());
      }
  
  }
  </code></pre>
  
  <p>Noting that in Abstract.java class</p>
  
  <pre><code>@SpringApplicationConfiguration(classes = Application.class)
  </code></pre>
  
  <p>was replaced by</p>
  
  <pre><code>@SpringBootTest(classes=UserController.class)
  </code></pre>
  
  <p>So when i run a unit test i get the following errors:</p>
  
  <p><strong>Stack trace:</strong></p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:201)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
      at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
      at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:255)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:111)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:148)
      at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
      at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
      at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
      at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
      at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  Caused by: java.lang.IllegalArgumentException: Can not build an ApplicationContext with a NULL 'contextLoader'. Consider annotating your test class with @ContextConfiguration.
      at org.springframework.util.Assert.notNull(Assert.java:115)
      at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:169)
      at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:197)
      ... 17 more
  </code></pre>
  
  <p>Any help concerning this?</p>