我正在尝试在Java Spring项目中使用EasyMock 3.4。我已经成功地模拟了所有对象并测试了除了使用JDBCTemplate的DAO之外的类。
@RunWith(EasyMockRunner.class)
public class DummyDAOImplTest extends EasyMockSupport {
@TestSubject
private DummyDAOImpl dummyDAOImpl = new DummyDAOImpl ();
JdbcTemplate jdbcTemplateObject;
@Before
public void setUp(){
jdbcTemplateObject = EasyMock.createNiceMock(JdbcTemplate.class);
dummyDAOImpl.setJdbcTemplate(jdbcTemplateObject);
}
@Test
public void testGetApplicationConfigValueReturnNonNull(){
String query = "SELECT value FROM application_configuration WHERE tag=?";
String tag = "REFRESH_INTERVAL";
EasyMock.expect(jdbcTemplateObject.queryForObject(query,new Object[] {tag}, String.class)).andReturn("12");
EasyMock.replay(jdbcTemplateObject);
Assert.assertEquals(12,dummyDAOImpl.getApplicationConfigValue(tag));
}
}
public class ConfigurationDAOImpl implements ConfigurationDAO {
private JdbcTemplate jdbcTemplateObject;
@Override
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplateObject = jdbcTemplate;
}
@Override
public int getApplicationConfigValue(String tag) {
String query = "SELECT value FROM application_configuration WHERE tag=?";
String refreshTime = jdbcTemplateObject.queryForObject(query,new Object[] {tag}, String.class);
if(refreshTime != null && !"".equals(refreshTime))
return new Integer(refreshTime);
else
return 0;
}
}
虽然在方法 testGetApplicationConfigValueReturnNonNull - 我试图模仿它返回12但它总是返回null。
这是我第一次使用EasyMock。我已经尝试过的任何我想念的东西都无法破解它!
最诚挚的问候, 孙大信
答案 0 :(得分:1)
事实上,你唯一的问题是你的期望线。它应该是
EasyMock.expect(jdbcTemplateObject.queryForObject(eq(query), aryEq(new Object[] {tag}), eq(String.class))).andReturn("12");
默认情况下,EasyMock会对参数执行equals
以符合预期。事情是没有为数组定义equals
。所以你需要为数组指定匹配器(aryEq
)。一旦你有一个争论的匹配器,你需要为所有这些都有一个(出于技术原因)。
下面简要介绍了完整的代码。
ConfigurationDAO
replayAll
,因为您正在扩展EasyMockSupport @Mock
EasyMockRunner
verifyAll
。它确保使用所有期望代码:
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
@RunWith(EasyMockRunner.class)
public class DummyDAOImplTest extends EasyMockSupport {
@TestSubject
private ConfigurationDAOImpl dao = new ConfigurationDAOImpl();
@Mock
JdbcTemplate jdbcTemplateObject;
@Test
public void testGetApplicationConfigValueReturnNonNull(){
String query = "SELECT value FROM application_configuration WHERE tag=?";
String tag = "REFRESH_INTERVAL";
expect(jdbcTemplateObject.queryForObject(eq(query), aryEq(new Object[] {tag}), eq(String.class))).andReturn("12");
replayAll();
assertEquals(12, dao.getApplicationConfigValue(tag));
verifyAll();
}
}
答案 1 :(得分:0)
可能你需要加载Spring上下文
@ContextConfiguration(locations = "classpath:application-context-test.xml")
@RunWith(EasyMockRunner.class) public class DummyDAOImplTest extends EasyMockSupport { ...
像这样覆盖你的appplication-context
应用程序上下文的test.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context/ http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx/ http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"><import resource="application-context.xml"/><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:your-db-conection" />
<property name="username" value="" />
<property name="password" value="" />