使用Mockito创建Mock Dao时获得null响应

时间:2016-09-16 08:38:33

标签: unit-testing spring-mvc mockito dao

我正在尝试为Dao类创建一个模拟数据。测试用例正在成功运行,但它返回的是空数据。我搜索并实现了@Mock,@ InjectMocks,Inititated MockitoAnnotation但仍然无法正常工作。该项目是在春季。上下文路径也是正确的。我没有使用任何其他方法。首先运行我试图只是调用一个方法并打印。请帮我解决这个错误。

RegionManager类:

@Service("regionManager")
public class RegionManager implements RegionManagerIntf {

    @Autowired
    RegionDaoIntf regionInquiry;

    private RegionDao regionDao;

    @Override
    public ListPojo retrieveData(String Id, String details, String code) {
        return regionInquiry.retrievePData(Id, details, code); 
    }

    public RegionDao getRegionDao() {
        return regionDao;
    }

    public void setRegionDao(RegionDao regionDao) {
        this.regionDao = regionDao;
    }


}
道上课:

@Component
public class RegionProcessorFactory implements RegionProcessorIntf {

    private static final Logger logger = Logger
            .getLogger(RegionProcessorFactory.class);

    @Override
    public ListPojo retrieveData(String Id,
            String details, String code) {
            ListPojo listPojo = new ListPojo();
            //Do some action
            return listPojo;
            }
    }

ListPojo:

It contains getter setters.

测试类:

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.test.context.ContextConfiguration;

import com.fasterxml.jackson.databind.ObjectMapper;

@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration({"classpath*:spring/beanRefContext.xml"})
public class RegionManagerTest
{
    private String Id = "12345";
    private String Code = "123";
    private String details = "12";

    ObjectMapper mapper;

    @Mock
    private RegionProcessorFactory dao;

    @Mock
    private ListPojo listPojo;

    @InjectMocks
    private RegionManager service;

    /**
     * Before method will be called before executing every test case
     */
    @Before
    public void initialize() {
        System.out.println("In initialize");
        MockitoAnnotations.initMocks(this);
        dao = mock(RegionProcessorFactory.class);
        listPojo = mock(ListPojo.class);

        service = new RegionManager();
        service.setRegionDao(dao);  
    }

    @Test
    public void CreateDatabaseMock() throws Exception
    {
        System.out.println("dao result :: "+dao.retrieveData(Id, "", ""));

        when(dao.retrieveData(Id, "", "")).thenReturn(listPojo);

        verify(dao).retrieveData(Id, "", "");
    }

    /**
     * After method will be called after executing every test case
     */
    @After
    public void TearDownClass() {
    }
}

1 个答案:

答案 0 :(得分:0)

首先:如果您使用的是@RunWith(MockitoJUnitRunner.class),那么here

就不需要MockitoAnnotations.initMocks(this);

第二个:@Mock的所有内容都将被嘲笑,mockito会尝试将其注入使用@InjectMocks注释的对象中,mockito会实例化(在旧的mockito版本中你必须自己创建对象)所以关注不需要行:

dao = mock(RegionProcessorFactory.class);
listPojo = mock(ListPojo.class);

service = new RegionManager();
service.setRegionDao(dao);  

第三:实际执行应该在存根后进行

@Test
public void CreateDatabaseMock() throws Exception{
    when(dao.retrieveData(Id, "", "")).thenReturn(listPojo);
    System.out.println("dao result :: "+dao.retrieveData(Id, "", ""));
    verify(dao).retrieveData(Id, "", "");
}