0

I want to junit test on a private class nest into the public class.

public class getData{
...
private class DataInfo {
 private String data;
 public String getData(){
  return data
}
}

}

Is there a way to test above private class method?

lin
  • 167
  • 1
  • 3
  • 13

3 Answers3

0

If you use JUnit, do it the JUnit way. You can't test private class, this link may a bit similar to the issue you are facing.

frianH
  • 7,295
  • 6
  • 20
  • 45
0

One of the mains reasons to make a class private is so that it can't be used somewhere else. This is so that if you want to change how the private class works, or even entirely delete it, you don't have to worry about potentially breaking other code. Unit testing private code generally defeats that purpose - when you want to change that code, you do have to worry about breaking the unit tests and changing them. Often just testing the public class that the private class is inside of should be enough.

But... sometimes you do run into situation where you do want to test private classes or methods. In those cases, this answer should give you a good idea of where to start: How do I test a private function or a class that has private methods, fields or inner classes?

Brendan Burkhart
  • 399
  • 5
  • 11
-1

Maybe you can achieve it by using reflection. Refer this: test using reflection