0

I know that others have asked this (or similar) questions but none of the solutions there have helped me. I have 3 classes:

public class A {
    public int superMethod(int val) {
        B b = new B();
        return b.subMethod(val);
    }
}
public class B{
    public int subMethod(int num) {
        return num;
    }
}

My test class:

public class MockitoTest {
    @Mock
    B b;
    
    @InjectMocks
    A a;
    
    @Test
    public void testMethodA(){
        a = mock(A.class);
        doCallRealMethod().when(a).superMethod(anyInt());
        b = mock(B.class);
        try {
            whenNew(B.class).withAnyArguments().thenReturn(b);
        } catch (Exception e) {}
        
        a.superMethod(5);
        
        verify(b).subMethod(anyInt());
    }
}

I want to verify that B.subMethod() is called within A.superMethod(). How can I accomplish this. I know that I need to use PowerMock for this but I am not sure how. Also, I am not allowed to change anything about class A or B.

Any help would be greatly appreciated!

Fluffy Cow
  • 63
  • 1
  • 7

1 Answers1

0

The problem I had was with my annotations (or lack thereof). I was missing the @PrepareForTest({A.class}) annotation on my MockitoTest class.

Fluffy Cow
  • 63
  • 1
  • 7