3

I am trying to use a mock Intent object but when using whenNew from PowerMockito, I am not able to mock the constructor. I have tried all possible combination of Arguments, but it just doesn't work.

Nitin Jain
  • 197
  • 1
  • 9

1 Answers1

7

I had a similar problem, and solution was found in this answer.

To be more specific: please try to add @PrepareForTest annotation at test or at class level, and provide it with class that constructs your Intent.

public class SomeClassThatCreatesIntent {

    public void someMethodWithIntent() {
        Intent i = new Intent();
    }
}

And then test class should look like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest({SomeClassThatCreatesIntent.class})
public class SomeClassThatCreatesIntentTest {

    @Test
    public void test() {
        // Some test that uses PowerMockito.whenNew(Intent.class)
    }
}

Hope this will help.

Community
  • 1
  • 1
Yaroslav
  • 205
  • 3
  • 7