Hello I’m writting junit test how can I test this method .. this is only part of this method :
public MyClass{
public void myMethod(){
List<myObject> list = readData();
}
}
How will I make the test for this? ReadData is a private method inside MyClass?
As written, it doesn’t make sense to test
myMethod()unlessreadData()changes the instance state the way Frank Grimm mentioned. One thing to do would be to changemyMethod()so that it putslistinto aListinstance variable. Then you might do something like this:You’d still have to write
MyClass.getList()to return theListinstance variable.To be robust, you could make the
MyClassconstructor accept an object that implements an interface likeIMyReadInterface.readData()would use that object. Then in your test, you could instantiate a mock that also implementsIMyReadInterface, configure the mock to provide the data needed so thatreadData()works correctly, and constructinstwith that mock.