Question

I am using TestNG, EasyMock and PowerMock for testing. As per the code below I am trying to mock a static method that is called from the static method under test (fetchAuthenticator). When I run the test, the executeHttpGet method gets executed when calling EasyMock.expect.

@PrepareForTest(Metadata.class)
public class MetadataTest extends PowerMockTestCase {
@Test
public void testPatience(){
PowerMock.mockStatic(HttpHelper.class);
EasyMock.expect(
    HttpHelper.executeHttpGet(EasyMock.anyString()))
    .andReturn(
        "{\"response\":\"some_value\"}");
PowerMock.replay(HttpHelper.class);

String response = Whitebox.invokeMethod(Metadata.class,
    "fetchAuthenticator",
    "something-else",
    "somesite.com", "another-value");
assertNotNull(response);
    }
}

I found similar questions, but no answers. EasyMock: Actual function gets called while passing as an argument to EasyMock.expect

Était-ce utile?

La solution

You forgot to include:

@RunWith(PowerMockRunner.class)

at the class-level of the test case

and replace

@PrepareForTest(Metadata.class)

with

@PrepareForTest({ AuthenticationMetadata.class, HttpHelper.class })
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top