12

Can we do unit testing of private functions and fields for Android Application using Android Mock ?

If yes, please explain how ?

Sachchidanand
  • 1,291
  • 2
  • 18
  • 35

4 Answers4

9

Unit testing a private method , just sounds a bit wrong to me . Public and protected methods are the candidates for unit testing. Just to test private methods , you can make the method public or create more tests of the public methods which call the private method, and tests the private method's core functionality.

rfsk2010
  • 8,571
  • 4
  • 32
  • 46
  • 1
    I agree with rfsk2010. If you find that it's too much work to test everything through the public method, it can be a code smell that your class is too large and has too many jobs. At that point you can look into breaking out your code into classes that have a single responsibliity. Often times you'll find these classes resuable and much easier to isolation unit test. – Scott Jan 12 '12 at 11:40
  • 1
    It is nice to be able to test internal methods as well. Although programming by contract makes your right, programming is based on divide and conqueer as well and there is no reason not want to test a private or protected method. I downvoted as the reason you gave is just too limited and doesn't address all needs expressed by developpers. – Snicolas Sep 03 '12 at 11:21
  • Best and proper legal way to test private method from test framework is @VisibleForTesting annotation over the method so same method will be visible for test framework as like public method – Android is everything for me Nov 23 '17 at 11:32
2

Hi posted a blog article about this topic and show how you can achieve to test internal methods by understanding the difference between a java package and AndroidManifest "package".

You will end up using the same trick as we do traditionally in Java : let methods to be tested be protected.

I hope that helps !

Snicolas
  • 37,840
  • 15
  • 114
  • 173
  • does the point no.4 in your blog article still eligible for AGP 3.6.x & above? I've commented out there – mochadwi May 02 '20 at 18:16
2

One year after, I also pushed a library to help testing private methods and fields. I believe that on Android there is still a need to test private methods.

You wanna make your activities' methods private to prevent other classes from thinking they can access it (fragment can, but that is wrong practice to me, it's better to use an observable-observer pattern). Then you will end up with private fields and methods that would need to be accessed by tests only.

BoundBox does exactly that ! Here below is an example of a test that accesses 2 private fields of an activity to test it :

@UiThreadTest
public void testCompute() {
    // given
    boundBoxOfMainActivity = new BoundBoxOfMainActivity(getActivity());

    // when
    boundBoxOfMainActivity.boundBox_getButtonMain().performClick();

    // then
    assertEquals("42", boundBoxOfMainActivity.boundBox_getTextViewMain().getText());
}
Snicolas
  • 37,840
  • 15
  • 114
  • 173
1

@VisibleForTesting(otherwise = VisibleForTesting.PROTECTED) private String field;

you can use Public and protected fields or methods in unit tests.

Vinay Kumar
  • 101
  • 1
  • 2