mardi 5 avril 2016

Mocking a static method with PowerMock, how can I have it accept any string parameter?

I am mocking a static method by using PowerMockRunner. Basically, the last line in the test method there is going to call AppUtil.appInstalled() many times with a Context object, and random String objects. I want it to return false, no matter what String is supplied.

@RunWith(PowerMockRunner.class)
@PrepareForTest(AppUtil.class)
public class StoreApiTests extends BaseTest{
    @Test
    public void testStoreRequestMapping() throws Exception{
        mockStatic(AppUtil.class);
        expect(AppUtil.appInstalled(context,anyString())).andReturn(false);
        replayAll();
        File file = getFileFromPath(this, "mock_store_response.json");
        server.enqueue(new MockResponse().setResponseCode(200).setBody(getStringFromFile(file)));

        IStoreApi service = retrofit.create(IStoreApi.class);


        Response<StoreResponse> response = service.store("","","","","","","",null,null,null).execute();

    }

I've tried the anyString() matcher, but I get this runtime error.

java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
This exception usually occurs when matchers are mixed with raw values when recording a method:
    foo(5, eq(6));  // wrong
You need to use no matcher at all or a matcher for every single param:
    foo(eq(5), eq(6));  // right
    foo(5, 6);  // also right

I've tried to follow the error message and used this variation, since apparently you need to use only matchers or none at all as the error says.

    expect(AppUtil.appInstalled((Context)anyObject(),anyString())).andReturn(false);

But I get the same error. How can I accept any String as a second parameter, and return false every time AppUtil.appInstalled() is called?

Aucun commentaire:

Enregistrer un commentaire