0

I have a switch case statement inside a private method. I don't think we need to do unit testing for the private method. But my code coverage tool(EclEmma) is showing "1 of 4 branches missed." with yellow diamond on switch statement. So my question is: how I can write test for this particular situation?

Code Snippet

public void parentMethod() {
  ....
  childMethod(someList);
  ....
} 

private void childMethod(List<Integer> someList) {
  for(Integer var : someList) {
    switch(var){ ..... }
  }
}
Community
  • 1
  • 1
αƞjiβ
  • 3,056
  • 14
  • 58
  • 95

1 Answers1

1

So, you can try to use reflection, something like this:

MyClass myClass = new MyClass();  
List<Integer> input = Arrays.asList(1, 2, 3);

Method method = MyClass.class.getDeclaredMethod("childMethod", List.class);
method.setAccessible(true);
method.invoke(myClass, input);
....

Good links about approachs for testing private methods or not testing in general:

http://saturnboy.com/2010/11/testing-private-methods-in-java/

http://www.artima.com/suiterunner/privateP.html

Alexey Semenyuk
  • 3,263
  • 2
  • 32
  • 36