0

Let's say I have a test class with some mocks in it. Lint suggests that the fields can be private. I'm wondering, is there any problem in making them private? One of my colleagues thinks that perhaps it will cause Mockito to use reflection. Is this true or can I safely mark them as private?

@Mock private Context context;

vs

@Mock Context context;

The official Android documentation has private on the Kotlin version but not on the Java version (at the time of writing this question). https://developer.android.com/training/testing/unit-testing/local-unit-tests#kotlin

I've looked around and Googled but I'm not sure since it's not mentioned explicitly anywhere that I could find, and some references have private while others don't.

The tests run fine in both cases and take around the same time (10 seconds). So for that reason I think it's better to mark as private and remove the lint error. What could possibly be the downside?

As far as I can tell, reflection has nothing to do with the value being private or not. It seems that it's more about whether it can see the methods inside the mock class. If those methods inside the class are private then it will need to use reflection.

Michael Vescovo
  • 3,741
  • 4
  • 32
  • 45
  • https://stackoverflow.com/a/51499019/4265739 – leonardkraemer Dec 31 '18 at 03:14
  • Thanks. I already looked at that post actually and I think they are referring to private fields within the person class. Not the person mock itself. I'm talking about setting private on the actual mock value in the test class (not the mocked object). – Michael Vescovo Dec 31 '18 at 03:36

2 Answers2

0

You can make the fields private if you want, reflection can make use of private fields as well.

I would personally think about best practices and have the variables as private with getter and setter methods if needed.

Richard Stokes
  • 385
  • 3
  • 11
  • I don't think it would be a good idea to use getters and setters in a test class; that would be unnecessarily complicated. I agree that they should be private but wanting some confirmation. E.g. if you have some documentation somewhere you can reference that at least confirms it indirectly. – Michael Vescovo Dec 31 '18 at 04:34
0

The "restriction" on reflection in Android only applies to code that runs on device in your application. Unit tests do not have to adhere to this principle as they are run on a computer and do not appear in your application.

I always use private properties in my test classes and have never run into any issues.

Update: After looking at the source code it seems that Mockito uses reflection to instantiate @mock properties whether they are private or public.

DanielO
  • 145
  • 1
  • 12
  • I agree that they should be private but as with my comment on the other answer I was wanting some confirmation. E.g. if you have some documentation somewhere you can reference that at least confirms it indirectly. I also have not run into an issue with it (except this strange comment on my PR when I went to make them private). – Michael Vescovo Dec 31 '18 at 04:35