0

I'm currently writing unit tests to test behavior of a method and would like to partially mock the methods calling injected properties. For example:

public void doSomething() {
  int complicatedInt = 1 + 1;
  if(getProperty().someBooleanReturn()) {
    ... etc. ... 
  }
}

So obviously I want to mock the getProperty() method in order to expect the someBooleanReturn(). My question is, since I don't want the getter to be visible to other classes, but visible to unit tests, I've currently been making these methods package-private (default scope). Is there a standard for these types of operations?

Thanks!

  • That looks reasonable... and not bad. There's a standard refactoring in eclipse for making all variable access for the object through the getters / setters. I might go protected in case you ever subclass it and people won't fuss about it being without a modifier. – user40980 Aug 22 '13 at 21:29
  • Thinking there's reasons for both protected and default, just depending on how you're using the class! Thanks for the comment, rep Wisconsin! =) – dardo Aug 22 '13 at 21:38
  • Consider also asking codereview.SE about this with actual (rather than faked) code. For example Review of the testing code that I have written using easymock/junit – user40980 Aug 22 '13 at 21:41
1

Having a class and a test class in the same package (possibly in different jars), combined with default visibility, is a standard way to get access to a class internals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.