I have a class ClassToTest which has a dependency on ClassToMock.
public class ClassToMock {
private static final String MEMBER_1 = FileReader.readMemeber1();
protected void someMethod() {
...
}
}
The unit test case for ClassToTest.
public class ClassToTestTest {
private ClassToMock _mock;
@Before
public void setUp() throws Exception {
_mock = mock(ClassToMock.class)
}
}
When mock is called in the setUp() method, FileReader.readMemeber1(); is executed. Is there a way to avoid this? I think one way is to initialize the MEMBER_1 inside a method. Any other alternatives?
Thanks!
Your
ClassToMocktightly coupled withFileReader, that’s why you are not able to test/mock it. Instead of using tool to hack the byte code so you can mock it. I would suggest you do some simple refactorings to break the dependency.Step 1. Encapsulate Global References
This technique is also introduced in Michael Feathers’s wonderful book : Working Effectively with Legacy Code.
The title pretty much self explained. Instead of directly reference a global variable, you encapsulate it inside a method.
In your case,
ClassToMockcan be refactored into this :then you can easily using Mockito to mockgetMemberOne().UPDATED Old Step 1 cannot guarantee
Mockitomock safely, ifFileReader.readMemeber1()throw exception, then the test will failled miserably. So I suggest add another step to work around it.Step 1.5. add Setter and Lazy Getter
Since the problem is
FileReader.readMember1()will be invoked as soon asClassToMockis loaded. We have to delay it. So we make the getter callFileReader.readMember1()lazily, and open a setter.Now, you should able to make a fake
ClassToMockeven withoutMockito. However, this should not be the final state of your code, once you have your test ready, you should continue to Step 2.Step 2. Dependence Injection
Once you have your test ready, you should refactor it further more. Now Instead of reading the
MEMBER_1by itself. This class should receive theMEMBER_1from outside world instead. You can either use a setter or constructor to receive it. Below is the code that use setter.These two step refactorings are really easy to do, and you can do it even without test at hand. If the code is not that complex, you can just do step 2. Then you can easily test
ClassToTestUPDATE 12/8 : answer the comment
See my another answer in this questions.