简单的Mockito测试(Spring + Dao)

时间:2017-01-13 18:32:51

标签: java spring mockito

我感到非常沮丧,因为我无法解决这个问题。我已经阅读了数百万篇文章,并且很清楚如何做到这一点。我正在尝试为练习写一个Mockito测试。

我有一个spring java app,它从数据库中获取数据。

这是applicationContext.xml

<bean id="Lookup" class="com.gd.test.Lookup">
    <property name="AssetClassDao" ref="assetClass" />
</bean>

<bean id="assetClass" class="com.gd.impl.AssetClassImpl">
    <property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="org.apache.commons.dbcp.BasicDataSource" />
    <property name="url"
        value="jdbc:sqlserver://ip:port;databaseName=test" />
    <property name="username" value="user" />
    <property name="password" value="pass" />
</bean>

AssetClassDao.java

public interface AssetClassDao {

     public AssetClass getAssetDataById(int id);
}

AssetClassImpl.java

public class AssetClassImpl implements AssetClassDao {

private DataSource dataSource;
AssetClass ac = new AssetClass();

@Override
public AssetClass getAssetDataById(int id) {

    String sql = "select id, name from TABLE where id="
            + String.valueOf(id);

    Connection conn = null;

    try {
        conn = dataSource.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);

        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            ac.setASSET_CLASS_ID(rs.getString("id"));
            ac.setASSET_CLASS_NAME(rs.getString("name"));
        }

        rs.close();
        ps.close();
        return ac;

    } catch (SQLException e) {
        e.printStackTrace();
    }

    return ac;
}

public DataSource getDataSource() {
    return dataSource;
}

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}
}

Lookup.java

public class Lookup {

private AssetClassDao assetClassDao;

public void runNameLookup() throws IOException {

    AssetClass byOne = assetClassDao.getAssetDataById(5);

    System.out.println(byOne.toString());

}

public AssetClassDao getAssetClassDao() {
    return assetClassDao;
}

public void setAssetClassDao(AssetClassDao assetClassDao) {
    this.assetClassDao = assetClassDao;
}

}

查询返回&#34; 5&#34;作为ID和&#34; Mortgage&#34;作为NAME。 对于我在Mockito上写的测试,它如何知道要使用哪种实现?

我的测试:

public class TestLookup {
@Mock
private AssetClassDao acd;

@Mock
private Lookup al;

@Mock
private AssetClass ac;

@Before
public void setupMock(){
    MockitoAnnotations.initMocks(this);
    acd = mock(AssetClassImpl.class);
}

@Test
public void testDataById(){
    when(acd.getAssetDataById(5)).thenReturn(ac);
    System.out.println(acd.getAssetDataById(5).getNAME());
}
}

如何正确测试这些值?!

1 个答案:

答案 0 :(得分:2)

通过编写以下语句,您将创建类AssetClass的新对象:

AssetClass ac = new AssetClass();

如果您正在使用Spring,那么您应该将其作为依赖项之一注入,如下所示:

@Autowired
private AssetClass ac;

现在,您可以通过在测试类中编写以下内容来模拟此依赖项:

@Mock
private AssetClass ac;

此外,您还必须模拟其他依赖项,例如DataSourceConnection等,并执行以下操作:

@Mock
private DataSource dataSource;

@Mock
private Connection conn;

...

Mockito.when(dataSource.getConnection()).thenReturn(conn);

我希望这能为您提供Mockito的一些精华以及如何使用它。