Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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!

share|improve this question
    
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. –  MichaelT 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 –  MichaelT Aug 22 '13 at 21:41
    
Noted! Thanks for the heads up. –  dardo Aug 22 '13 at 21:52
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks, will continue what I'm doing then! –  dardo Aug 22 '13 at 21:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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