Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:06:10+00:00 2026-06-15T16:06:10+00:00

I have a class ClassToTest which has a dependency on ClassToMock. public class ClassToMock

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:06:12+00:00Added an answer on June 15, 2026 at 4:06 pm

    Your ClassToMock tightly coupled with FileReader, 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, ClassToMock can be refactored into this :

    public class ClassToMock {
      private static final String MEMBER_1 = FileReader.readMemeber1();
    
      public String getMemberOne() {
        return MEMBER_1;      
      }
    }
    

    then you can easily using Mockito to mock getMemberOne().

    UPDATED Old Step 1 cannot guarantee Mockito mock safely, if FileReader.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 as ClassToMock is loaded. We have to delay it. So we make the getter call FileReader.readMember1() lazily, and open a setter.

    public class ClassToMock {
      private static String MEMBER_1 = null;
    
      protected String getMemberOne() {
        if (MEMBER_1 == null) {
          MEMBER_1 = FileReader.readMemeber1();
        }
        return MEMBER_1;      
      }
    
      public void setMemberOne(String memberOne) {
        MEMBER_1 = memberOne;
      }
    }
    

    Now, you should able to make a fake ClassToMock even without Mockito. 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_1 by itself. This class should receive the MEMBER_1 from outside world instead. You can either use a setter or constructor to receive it. Below is the code that use setter.

    public class ClassToMock {
      private String memberOne;
      public void setMemberOne(String memberOne) {
        this.memberOne = memberOne;
      }
    
      public String getMemberOne() {
        return memberOne;
      }
    }
    

    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 ClassToTest


    UPDATE 12/8 : answer the comment

    See my another answer in this questions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have class with back reference: public class Employee : Entity { private string
I have class with collection as below public class MyClass:IXmlSerializable { int vesrion; private
I have the following code public ClassToTest : IClassToTest { private readonly DBRepository rep;
Say I have the following code: public class ClassToTest { AnotherClass anotherClass; public void
I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
I have class which calls a static class, the static class basically is wrapper
I have class(Customer) which holds more than 200 string variables as property. I'm using
I have class that has a property that is of type string[]. I need
I have class like this: public class object { [Key] int number; String mystring;
I have Class as defined below class Employee { public int EmpNo{get;set;} public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.