超级方法调用的Mockito NullPointerException

时间:2016-04-05 23:05:37

标签: java spring mockito junit4

我有以下细分类(为了简单起见,我们假设Service接口只有单个overriden方法)。无论我尝试什么,当ServiceImpl调用getSqlSession()时,我总是会得到NullPointerException。如何覆盖此调用以避免异常?我尝试创建一个间谍ServiceImpl并在方法when上调用getSqlSession(),但我仍然得到异常。我不希望将ServiceImpl.getData()中的逻辑放入将List<Object>更改为Map<String,Object>到测试类中,因为如果此实现发生更改,则还需要更改测试代码。有什么建议吗?

public class ServiceImpl extends BaseService implements Service {

    @Autowired
    private ServiceDAO serviceDao;

    @Override
    public Map<String, Object> getData() {
        Map<String,Object> map = new HashMap<String,Object>();
        List<Object> resultSet = service.getData(getSqlSession());
        map.put("Test", resultSet.get(0));
        map.put("Test2", resultSet.get(1));

        return map;
    }

   public void setServiceDao(final ServiceDAO serviceDao) {
     this.serviceDao = serviceDao;
   }

}

BaseService:

public class BaseService {

    @Autowired
    private ApplicationContext context;

    public SqlSession getSqlSession() {
        return (SqlSession)context.getBean("SQL_SESSION");
    }

    public void setContext(ApplicationContext context) {
        this.context=context;
    }

}

DAO

public class ServiceDAO {
    public List<Object> getData(SqlSession sqlSession) {
        return sqlSession.selectList("proc_name");
    }
}

测试类:

public class ServiceImplTest {

    private Service service;

    @Mock
    private ServiceDAO serviceDao;

    @Before
    public void before() {
        MockitoAnnotations.initMocks(this);
        service = new ServiceImpl();
        service.serviceDao = this.serviceDao;
    }

    @Test
    public void testGetData() {
        Mockito.when(service.getData()).then(new Answer<List<Object>>() {

        @Override
        public List<Object> answer(InvocationOnMock invocation) throws Throwable {
            return serviceDAO.getData(new DefaultSqlSession(null,null));

        }

    });

        service.getData();

    }
}

2 个答案:

答案 0 :(得分:0)

我相信当您尝试使用服务中的serviceDao时会发生错误:

List<Object> resultSet = service.getData(getSqlSession());

如果您在同一个软件包中,则需要在服务中注入模拟的serviceDAO:

@Before
public void before() {
    MockitoAnnotations.initMocks(this);
    service = new ServiceImpl();
    service.service = this.serviceDAO;
}

答案 1 :(得分:0)

终于能够使用下面的测试课了。不得不将接口的Service声明更改为实际的Impl类。不确定是否有任何解决方法,因为所有其他尝试都没有奏效。

public class ServiceImplTest {

    @Mock
    private ServiceDAO serviceDAO;

    @Spy
    private ServiceImpl service;

    @Before
    public void before() {
        MockitoAnnotations.initMocks(this);
        //service = new ServiceImpl();
        service.setServiceDAO(this.serviceDAO);
    }

    @Test
    public void testGetData() {
        Mockito.doAnswer(new Answer<SqlSession>(){

            @Override
            public SqlSession answer(InvocationOnMock invocation) throws Throwable {
                return new DefaultSqlSession(null,null);
            }

        }).when(service).getSqlSession();

        Mockito.when(service.getData()).then(new Answer<List<Object>>() {

        @Override
        public List<Object> answer(InvocationOnMock invocation) throws Throwable {
            return serviceDAO.getData(new DefaultSqlSession(null,null));

        }

    });

        service.getData();

    }

}