Reputation: 205
I am beyond frustrated because I can't figure this out. I've been over millions of articles and NOTHING is clear on how this is done. I am trying to write a Mockito test just for practice.
I have a spring java app which is getting data from a database.
Here is the 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;
}
}
The query returns "5" as the ID and "Mortgage" as the NAME. For my test that I am writing with Mockito, how does it know which implementation to use?
My test:
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());
}
}
How do I test these values properly?!
Upvotes: 2
Views: 2149
Reputation: 9648
By writing the following statements you are creating a new object of class AssetClass
:
AssetClass ac = new AssetClass();
If you are using Spring then you should be injecting this as one of the dependencies as follows:
@Autowired
private AssetClass ac;
You can now mock this dependency out by writing the following in your test class:
@Mock
private AssetClass ac;
Also, you'll have to mock the other dependencies like DataSource
, Connection
etc. and do something like this:
@Mock
private DataSource dataSource;
@Mock
private Connection conn;
...
Mockito.when(dataSource.getConnection()).thenReturn(conn);
I hope this will provide you some essence of Mockito and how to use it.
Upvotes: 2