0

I have write a small test case for spring mvc data access layer.

Test class

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<UserAccount> userAccountList=homeDao.getUserAccountList("1");
        System.out.println("userAccountList  :"+userAccountList);
    }
}

Dao class

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<UserAccount> getUserAccountList(String userId) throws Exception {
        List<UserAccount> userAccountList=new ArrayList<UserAccount>();
        try{
            List<Map<String, Object>> resultSet=jdbcTemplate.queryForList(sqlUserAccountList,new Object[] {userId,commonVarList.STATUS_DEFAULT_ACTIVE});
            if(!resultSet.isEmpty()){
                for(Map<String,Object> 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;
    }
}

I inject the needed objects as mock objects in test class.

But some values of the mock object are gain using the property file and it is configured in appication-context xml file.

I tried to load appication-context xml file before test case but it was failed. I tried as below.

@ContextConfiguration(locations = {"/springjbehave-servlet.xml"})

Project file structure

Project file structure

I have seen similar questions to this scenario. But the following suggestions does not fix the issue.

Is any one can describe why it happen and how to fix this issue it will be great helpful. Thanks in advance.

speeding-up-spring-integration-tests

How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?

Spring JUnit Test not loading full Application Context

4
  • 1
    Create a config for the test and mock the beans in the configuration file. See stackoverflow.com/questions/2457239/…. Or don't use Spring and just create an instance yourself and inject the mocks which would make it a pure unit-test (you don't have to use Spring for testing especially not if you want a simple unit test). Commented Apr 18, 2018 at 7:34
  • @Denium I have go through with the above provided link.But still it is confused for me.It will be really helpful if you can provide a small example to config the test and mock the beans in the configuration file. Commented Apr 18, 2018 at 10:05
  • That is all in the linked answer... Commented Apr 18, 2018 at 10:09
  • I will go through the link again and try.Thank you very much @Denium. Commented Apr 18, 2018 at 10:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.