title: JUnit test cases for Spring Service Layer
body: <p>I am trying to configure JUnit and write test cases for Service layer of Spring 3.2 MVC application. I couldnt find much information on how to configure the JUnit from scratch and make it work for Spring service layer. here is my problem</p>
  
  <p>I dont really know what version of junit to be used so i just grabbed the latest version</p>
  
  <p>Maven junit dependancy</p>
  
  <pre><code> &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.11&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>My service Class</p>
  
  <pre><code>@Service
  public class AuthService implements IAuthService, ApplicationContextAware,
          org.springframework.context.ApplicationListener&lt;org.springframework.context.event.ContextRefreshedEvent&gt; {
  
  public Collection&lt;? extends String&gt; addCommandPermissions(Session session, CommandMetadata command) {
  
      Set&lt;String&gt; result = new HashSet&lt;String&gt;();
      String commandName = command.getBeanName();
      String defaultAdministerPermission = command.getAdministerPermission()
      String defaultExecutePermission = command.getExecutePermission()
      String overrideAdminPermission = null;
      String overrideExecPermission = null;
      String finalAdministerPermission = overrideAdminPermission == null ? defaultAdministerPermission
              : overrideAdminPermission;
      command.setAdministerPermission(finalAdministerPermission);
      result.add(finalAdministerPermission);
      String finalFxecutePermission = overrideExecPermission == null ? defaultExecutePermission
              : overrideExecPermission;
      command.setExecutePermission(finalFxecutePermission);
      result.add(finalFxecutePermission);
      try {
          session.saveOrUpdate(command);
          session.flush();
      } finally {
          // TODO - more swallowed exceptions.
      }
      return result;
  }
  
  // some other methods
  }
  </code></pre>
  
  <p>My Test Class(used groovy partially)</p>
  
  <pre><code>import junit.framework.TestCase;
  import org.mockito.Mockito;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.mock.web.MockHttpSession;
  import org.springframework.test.context.ContextConfiguration;
  import com.dc.core.security.service.impl.AuthService
  import com.dc.core.behavior.command.model.impl.CommandMetadata;
  import org.hibernate.SessionFactory
  import org.hibernate.classic.Session
  
  @ContextConfiguration(locations = "file:WebContent/WEB-INF/applicationContext.xml")
  public class AuthServiceTest extends TestCase {
  
      @Autowired
      private AuthService authService;
  
      @Autowired
      private MockHttpSession mockHttpSession;
  
      @Autowired
      ApplicationContext appContext
  
      @Autowired
      SessionFactory sessionFactory
  
      private Session mockHibernateSession = Mockito.mock(org.hibernate.classic.Session.class);
  
      private CommandMetadata commandMetadata = new CommandMetadata();
  
      public void setUp() {
         appContext.getBeanFactory().registerScope("request", new RequestScope())
         MockHttpServletRequest request = new MockHttpServletRequest()
  
          ServletRequestAttributes attributes = new ServletRequestAttributes(request)
          RequestContextHolder.setRequestAttributes(attributes)
          CurrentRequestProperties currentRequestProperties = appContext.getBean("currentRequestProperties")
          session = sessionHandler.initiateSession(sessionFactory, currentRequestProperties)
  
  
      }
  
      public void testAddCommandPermissions() {
          commandMetadata.beanName = "TestBean"
          commandMetadata.administerPermission = "TestBean.administer"
          commandMetadata.executePermission = "TestBean.execute"
          Collection&lt;String&gt; results = authorizationService.addCommandPermissions(session, commandMetadata);
          assertTrue(results.contains("TestBean.administer"))
      }
  
      public void testCanary() {
          assertTrue(true);
      }
  }
  </code></pre>
  
  <p>When I run my test case am getting the below error
  java.lang.NullPointerException: Cannot invoke method getBeanFactory() on null object</p>
  
  <p>I think cause of the issue is appContext is not injected properly hence am getting NPE. But I couldnt solve this issue. I really appreciate someone's help on this</p>