# Whitebox.invokeMethod()

Used to call an private method, just a wrapper on Java Reflection API's

```java
// Example Source Class
public class ExampleClass {
    private int getHeight() {
        // Lets say we have a generic method which provides different result base on the provided id
        final CustomClass customClassInstance = new CustomClass();
        return customClass.getHeight();
    }
}
```

We can use `Whitebox.invokeMethod()` to execute that method, here an example:

```java
@RunWith(PowerMockRunner.class)
@PrepareForTest({
    ExampleClass.class
})
public class ExampleClassTest {

    private ExampleClass classUnderTest;

    @Before
    public void setUp() throws Exception {
        // Create constructor of ExampleClass
        classUnderTest = new ExampleClass();
    }

    @Test
    public void getHeight_shoudReturnHeightOfView() throws NoSuchMethodException {
        final int expectedHeight = 100;
        // Given
        final CustomClass mckCustomClass = Mockito.mock(CustomClass.class);
        Mockito.when(mckCustomClass.getHeight()).theReturn(expectedHeight);
        // Make sure you match exact parameters while mocking (same as we do for Mockito.mock statements).
        // There is also withArguments(first, second, ....)
        // and withAnyArguments() for cases when you dont care for arguments passed.
        PowerMockito.whenNew(CustomClass.class).withNoArguments().thenReturn(mckCustomClass);

        final int actualResult = Whitebox.invokeMethod(classUnderTest, "getHeight");;

        Assert.assertEquals(expectedHeight, actualResult);
    }
}
```

**Note:** you should not call `invokeMethod()` unnecessarily, try to follow code execution flow to get to any private method.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tejpratapsingh.com/_/java-testing/powermockito/whitebox.invokemethod.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
