Spring上的EasyMock jdbcTemplate总是返回null而非模拟对象

问题描述 投票:1回答:2

我正在尝试在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。任何我想念的东西,因为已经尝试过而且无法破解它!

最诚挚的问候,桑达尔

spring jdbctemplate easymock
2个回答
1
投票

事实上,你唯一的问题是你的期望线。它应该是

EasyMock.expect(jdbcTemplateObject.queryForObject(eq(query), aryEq(new Object[] {tag}), eq(String.class))).andReturn("12");

默认情况下,EasyMock将在参数上执行equals以符合预期。事情是没有为数组定义equals。所以你需要为数组指定匹配器(aryEq)。一旦你有一个争论的匹配器,你需要为他们所有人(由于技术原因)。

完整的代码有一些简化如下。

  1. 我认为你要测试的是ConfigurationDAO
  2. 您可以在扩展EasyMockSupport时使用replayAll
  3. 你可以使用@Mock因为跑步者
  4. 你不需要一个漂亮的模拟。事实上,在这里不使用它将显示一个很好的例外,关于意外的调用本来对你有用
  5. 模拟现在也由EasyMockRunner注入
  6. 我更喜欢在大多数测试结束时添加一个verifyAll。它确保使用所有期望
  7. 静态导入,因为我认为它更清晰

码:

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();
  }
}

0
投票

可能你需要加载Spring上下文

@ContextConfiguration(locations = "classpath:application-context-test.xml")
@RunWith(EasyMockRunner.class) public class DummyDAOImplTest extends EasyMockSupport { ...

像这样覆盖您的应用程序上下文

应用程序上下文的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="" />

© www.soinside.com 2019 - 2024. All rights reserved.