0

Let's say I have a class A, which has two methods. I have to test a method, which internally calls the other method.

class A {
    private void method1() {
        //some steps to be done
    }

    public void method3() {
     //some steps
    }

    public void method2() {
        method1();
        if (XUtil.isSupportRequired) {
            method3();
        }
    }
 }

So, If I try to test method2, how to mock method1, as it is not public. Any help would be appreciated. I am using junit and mockito.

1 Answers1

3

I think you should try to write black-box tests, that is, it should be of no concern what the class does internally only what is visible through its public interface.

So, test the effects of method 2, either through other methods that should reflect these effects, or through mocked dependencies that are used in the process.

You shouldn't need to mock that other private method.

Robert Bräutigam
  • 7,514
  • 1
  • 20
  • 38