title: How to load application context xml file before junit test class with mock objects
body: <p>I have write a small test case for spring mvc data access layer.</p>
  
  <p><strong>Test class</strong></p>
  
  <pre><code>package com.test.jbehave.steps;
  
  import java.util.List;
  import org.jbehave.core.annotations.BeforeScenario;
  import org.jbehave.core.annotations.Given;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.test.context.ContextConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  
  import com.test.dao.home.HomeDao;
  import com.test.dao.home.impl.HomeDaoImpl;
  import com.test.mapping.useraccount.UserAccount;
  import com.test.util.common.Common;
  import com.test.util.varlist.CommonVarList;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(locations = {"/springjbehave-servlet.xml"})
  public class UserAccountListSteps{
  
      @Mock
      JdbcTemplate jdbcTemplate;
  
      @Mock
      CommonVarList commonVarList;
  
      @Mock
      Common common;
  
      @InjectMocks
      HomeDao homeDao =new HomeDaoImpl();
  
      @BeforeScenario
      public void initMocks(){
          MockitoAnnotations.initMocks(this);
      }
  
      @Given("customer userid is $userid")
      @Test
      public void checkUserAccountList() throws Exception{
          List&lt;UserAccount&gt; userAccountList=homeDao.getUserAccountList("1");
          System.out.println("userAccountList  :"+userAccountList);
      }
  }
  </code></pre>
  
  <p><strong>Dao class</strong></p>
  
  <pre><code>package com.test.dao.home.impl;
  
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Scope;
  import org.springframework.jdbc.core.JdbcTemplate;
  import org.springframework.stereotype.Repository;
  import com.test.dao.home.HomeDao;
  import com.test.mapping.useraccount.UserAccount;
  import com.test.util.common.Common;
  import com.test.util.varlist.CommonVarList;
  
  @Repository
  @Scope("prototype")
  public class HomeDaoImpl implements HomeDao{
      private final Log logger = LogFactory.getLog(getClass());
  
      @Autowired
      JdbcTemplate jdbcTemplate;
  
      @Autowired
      CommonVarList commonVarList;
  
      @Autowired
      Common common;
  
      private final String sqlUserAccountList ="SELECT USERID,ACCOUNTNO,ACCOUNTTYPE,BANK,STATUS,ACC_HOLDER_NAME,BRANCH FROM USERACCOUNT WHERE USERID=? AND STATUS=?";
  
      @Override
      public List&lt;UserAccount&gt; getUserAccountList(String userId) throws Exception {
          List&lt;UserAccount&gt; userAccountList=new ArrayList&lt;UserAccount&gt;();
          try{
              List&lt;Map&lt;String, Object&gt;&gt; resultSet=jdbcTemplate.queryForList(sqlUserAccountList,new Object[] {userId,commonVarList.STATUS_DEFAULT_ACTIVE});
              if(!resultSet.isEmpty()){
                  for(Map&lt;String,Object&gt; record : resultSet){
                      UserAccount userAccount=new UserAccount();
  
                      userAccount.setUserId(common.replaceNullAndEmpty(record.get("USERID")));
                      userAccount.setAccountNumber(common.replaceNullAndEmpty(record.get("ACCOUNTNO")));
                      userAccount.setAccountType(common.replaceNullAndEmpty(record.get("ACCOUNTTYPE")));
                      userAccount.setBank(common.replaceNullAndEmpty(record.get("BANK")));
                      userAccount.setStatus(common.replaceNullAndEmpty(record.get("STATUS")));
                      userAccount.setAccountHolderName(common.replaceNullAndEmpty(record.get("ACC_HOLDER_NAME")));
                      userAccount.setBranch(common.replaceNullAndEmpty(record.get("BRANCH")));
  
                      userAccountList.add(userAccount);
                      System.out.println(userAccount.toString());
                  }
              }
          }catch(Exception e){
              logger.error("Exception  :  ", e);
              throw e;
          }
          return userAccountList;
      }
  }
  </code></pre>
  
  <p>I inject the needed objects as mock objects in test class.</p>
  
  <p>But some values of the mock object are gain using the property file and it is configured in appication-context xml file.</p>
  
  <p>I tried to load appication-context xml file before test case but it was failed.
  I tried as below.</p>
  
  <pre><code>@ContextConfiguration(locations = {"/springjbehave-servlet.xml"})
  </code></pre>
  
  <hr>
  
  <p><strong>Project file structure</strong></p>
  
  <h2><a href="https://i.stack.imgur.com/yF5d7.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yF5d7.jpg" alt="Project file structure"></a></h2>
  
  <p>I have seen similar questions to this scenario. But the following suggestions does not fix the issue.</p>
  
  <p>Is any one can describe why it happen and how to fix this issue it will be great helpful. Thanks in advance.</p>
  
  <p><a href="https://www.nurkiewicz.com/2010/12/speeding-up-spring-integration-tests.html" rel="nofollow noreferrer">speeding-up-spring-integration-tests</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2425015/how-to-access-spring-context-in-junit-tests-annotated-with-runwith-and-context">How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28712134/spring-junit-test-not-loading-full-application-context">Spring JUnit Test not loading full Application Context</a></p>