gpt4 book ai didi

java - 如何使用 EasyMock 模拟测试类中正在实例化的类?

转载 作者:行者123 更新时间:2023-12-01 10:01:41 24 4
gpt4 key购买 nike

我正在尝试测试一个有点复杂的程序。getDatesMi 调用同一类中的方法。该方法进行调用到一个接口(interface)方法,该方法返回转换为 MarketLimits 类型的查询结果。

我正在使用 EasyMock 和 junit。

我的问题是:

  1. 如何测试 getSqlMapClientTemplate().queryForObject ?
  2. 如何模拟 getSqlMapClientTemplate().queryForObject 的结果转换为 MarketLimits

我是 java 和 EasyMock 的新手,所以也许答案是显而易见的,但我们将不胜感激。

我目前在 subDates.greaterThenOne() 上收到 NullPointerException

MyServiceDao 接口(interface)

import com.foo.bar.domain.MarketLimits;

public interface MyServiceDao {
public MarketLimits getDates(long orgId) throws DataAccessException;
}

实现 MyServiceDao 的类

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.foo.bar.domain.MarketLimits;


public class myServiceImpl implements MyServiceDao{

@Override
public MarketLimits getDates(long orgId) throws DataAccessException {
return (MarketLimits) getSqlMapClientTemplate().queryForObject("retrieveLimits", orgId);
}

}

被测主类

import com.foo.bar.dao.MyServiceDao;

public class myServiceImpl extends reports implements myService {

@Autowired
private MyServiceDao myServiceDao;


public String getDatesMi() throws Exception {
MarketLimits subDates = getDates(23);

System.out.print(subDates); // this prints out null

if(subDates.greaterThenOne()){
// do some things
}

return "result"
}


@Override
public MarketLimits getDates(long orgId) {
return myServiceDao.getDates(orgId);
}


public setMyServiceDao(MyServiceDao myServiceDao){
this.myServiceDao = myServiceDao
}

}

我的测试

@RunWith(EasyMockRunner.class)
public class myServiceImplTest {

@org.easymock.Mock
private MyServiceDao myServiceDao;

@TestSubject
private MyServiceImpl myServiceImpl = new MyServiceImpl();

@Test
public void testGetDatesMi() throws Exception {

MarketLimits limits = new MarketLimits();
limits.greaterThenOne(true);
expect(myServiceDao.getDates()).andReturn(limits);
replay(myServiceDao);

myServiceImpl.getDatesMi(12);

}
}

更新 1

由于某种原因,正在创建 EasyMock 实例 myServiceDao,但是当在 EasyMock 实例上调用方法时,它将返回 NULL。我的解决方案是在 myServiceImpl 上创建一个 setter 方法来设置我的 myServiceDao 模拟。这是 Deendayal Garg 的建议

更新2

事实证明我不需要 setter =)我更改了标题以反射(reflect)我面临的真正问题。

最佳答案

好的。为了给出更完整的答案,我尝试了您的代码。类(class)之间存在很多不一致的地方。我尽我所能修复了它们,这是一个有效的示例。正如您将看到的,注入(inject) Dao 不需要 setter。

@RunWith(EasyMockRunner.class)
public class MyServiceImplTest {

@org.easymock.Mock
private MyServiceDao myServiceDao;

@TestSubject
private MyServiceImpl myServiceImpl = new MyServiceImpl();

@Test
public void testGetDatesMi() throws Exception {

MarketLimits limits = new MarketLimits();
limits.greaterThenOne(true);
expect(myServiceDao.getDates(23)).andReturn(limits);
replay(myServiceDao);

myServiceImpl.getDatesMi();

verify(myServiceDao);
}
}

public class MarketLimits {
private boolean b;

public void greaterThenOne(boolean b) {
this.b = b;
}

public boolean greaterThenOne() {
return b;
}
}

public interface MyServiceDao {
MarketLimits getDates(long orgId);
}

public class MyServiceImpl {

private MyServiceDao myServiceDao;


public String getDatesMi() throws Exception {
MarketLimits subDates = getDates(23);

System.out.print(subDates); // this prints out null

if(subDates.greaterThenOne()){
// do some things
}

return "result";
}

public MarketLimits getDates(long orgId) {
return myServiceDao.getDates(orgId);
}

public void setMyServiceDao(MyServiceDao myServiceDao){
this.myServiceDao = myServiceDao;
}

}

关于java - 如何使用 EasyMock 模拟测试类中正在实例化的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36774104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com