0

I am trying to create a mock data for Dao class. Test case is running successfully but it is returning null data. I searched and implemented @Mock, @InjectMocks, Inititated MockitoAnnotation but still it is not working. The project is in spring. Context path is also correct. I have not used any other methods. First for running I am trying to just call a method and print. Please help me to solve this error.

RegionManager Class:

@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;
    }


}

Dao Class:

@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.

Test Class:

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() {
    }
}
AIM
  • 101
  • 1
  • 13
  • what is the error that you are getting while executing this Junit? One possible error that I can see in this code is that you must write service = new RegionManager(); stmt before MockitoAnnotations.initMocks(this) – Shubhi224 Sep 16 '16 at 10:44

1 Answers1

2

First: If you are using @RunWith(MockitoJUnitRunner.class) there is no need for MockitoAnnotations.initMocks(this); more on that here

Second: Everything with @Mock will be mocked and mockito will try to inject it into object annotated with @InjectMocks which mockito will instantiate(in old mockito versions you had to create the object yourself) so following lines are not needed:

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

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

Third: The actual execution should come after stubbing

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