I am testing a class "DataSource" that extends another class "ExtendedDataSource". The method that I am testing in the "DataSource" class is calling another protected method in the "ExtendedDataSource" class.
class DataSource extends ExtendedDataSource {
@Override
protected Connection setup(Properties props) {
Connection connection = super.getConnection(props);
return connection;
}
}
class ExtendedDataSource extends AbstractSource {
@Override
protected Connection getConnection(Properties props) {
// here an user is retrieved from an abstract class that this class inherits
// In this line I am getting an exception IllegalStateException--"user not set"
String user = getUser();
// Does something with props
// Calls another protected method with the props as argument
anothermethod(props);
}
protected anothermethod(Properties props){
//call another method that will do something with props
}
}
public abstract class AbstractSource extends AnotherAbstractClass{
public String getUser() {
//the user as string
return user;
}
}
The test for the DataSource class is as follows :-
public class DataSourceTest {
@InjectMocks
DataSource dataSource;
@Mock
properties props;
@Test
public void datatest() {
DataSource dataSource = new DataSource();
dataSource.getConnection(props);
}
}
How should I get over this situation? Should I also mock the Abstract class? But in this way do I have to mok all the classes?