D
D
DeNissss44442021-08-27 11:25:33
Java
DeNissss4444, 2021-08-27 11:25:33

How to access a class method with the private static modifier from another class?

I have a conditional class A which has a private static method.

public class A{
 public static void main(String[] args) {
      String c = "abc";
      int result = methodB(c);
}

private static int methodB(String c) {
/ * */
}

}

The class is working and now I need to write tests to check and something I can’t figure out how I can write a test to check the "methodB" method if it has private static. Can you suggest what is the best thing to do in this situation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2021-08-27
@DeNissss4444

DeNissss4444 , you can (and should) test only what is "available to the world" from the class.
Private method tests are by definition unnecessary. Today there is a method, tomorrow it was removed, breaking the functionality into two parts. Or they changed the signature. Or ... in general, what's going on there "under the hood", in the privates of the class - it's only and exclusively for this class.
Only externally accessible methods matter.
Roughly speaking, if you have a public method for sorting an array with a sort() bubble, and the private method swap() is called in it, then you do not need to test whether the exchange works correctly. Test sorting. Because tomorrow you will rewrite sorting to something more kosher, and instead of swap() you will have some kind of join().
The only important thing is that sort() returns the correct value.

S
Sergey Gornostaev, 2021-08-27
@sergey-gornostaev

shoulditestprivatemethods.com

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question