通过 EasyMock 模拟的静态方法和 Powermock 在调用 EasyMock.expect() 时被调用



我正在使用TestNG,EasyMock和PowerMock进行测试。根据下面的代码,我正在尝试模拟从测试中的静态方法(fetchAuthenticator)调用的静态方法。当我运行测试时,executeHttpGet 方法在调用 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);
    }
}

我发现了类似的问题,但没有答案。EasyMock:实际函数在作为参数传递给 EasyMock.expect 时被调用

你忘了包括:

@RunWith(PowerMockRunner.class)

在测试用例的类级别

并替换

@PrepareForTest(Metadata.class)

@PrepareForTest({ AuthenticationMetadata.class, HttpHelper.class })

最新更新