How to mock static member variables

Posted by pkrish on Stack Overflow See other posts from Stack Overflow or by pkrish
Published on 2012-12-05T19:36:23Z Indexed on 2012/12/08 5:04 UTC
Read the original article Hit count: 163

Filed under:
|
|
|

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!

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about junit