# PowerMockito.replace()

`stub()` works best when we don't care about the parameters and want same result irrespective of parameter passed.

With `replace()` we can go one step ahead of `stub()`, depending upon the supplied parameters, we can return different results. Let's see another similar example:

```java
// Example Source Class
public class ExampleClass extends Screen {
    public int getHeaderView() {
        // Lets say we have a generic method which provides different result base on the provided id
        final TextView textView = findViewById(ViewFactory.text_view);
        textView.setText("Header");
        final ImageView imageView  = findViewById(ViewFactory.image_view);
        imageView.setImage(BitmapFactory.getUserProfileImage());
        return new HStack(textView, imageView);
    }
}
```

Now, if we want to test our method with dynamic values, maybe you need to return different values based on parameter.

```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 getHeaderView_shoudReturnHStack() throws NoSuchMethodException {
        // Given
        PowerMockito.replace(View.class.getDeclaredMethod("findViewById", int.class)).with(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                final int viewId = (int) args[0];

                if (viewId == ViewFactory.text_view) {
                    // return mock of TextView
                    return Mockito.mock(TextView.class);
                } else if (viewId == ViewFactory.image_view) {
                    // return mock of ImageView
                    return Mockito.mock(ImageView.class);
                }
                // else you can call real method as well
                return method.invoke(proxy, args);
            }
        });

        // When
        final HStack actualResult = classUnderTest.getHeaderView();

        Assert.assertNotNull(actualResult);
    }
}
```

**Note:** keep in mind, if you need to manipulate behavior (byte code) of any class, as we are doing in case of replace method, we need to add `ExampleClass.class` in `@PrepareForTest`.


---

# 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/powermockito.replace.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.
