gpt4 book ai didi

java - 是否可以在 PowerMock 中对私有(private)静态方法使用部分模拟?

转载 作者:IT老高 更新时间:2023-10-28 20:27:47 25 4
gpt4 key购买 nike

来自 PowerMock homepage 上的示例,我看到以下示例使用 Mockito 部分模拟私有(private)方法:

@RunWith(PowerMockRunner.class)
// We prepare PartialMockClass for test because it's final or we need to mock private or static methods
@PrepareForTest(PartialMockClass.class)
public class YourTestCase {
@Test
public void privatePartialMockingWithPowerMock() {
PartialMockClass classUnderTest = PowerMockito.spy(new PartialMockClass());

// use PowerMockito to set up your expectation
PowerMockito.doReturn(value).when(classUnderTest, "methodToMock", "parameter1");

// execute your test
classUnderTest.execute();

// Use PowerMockito.verify() to verify result
PowerMockito.verifyPrivate(classUnderTest, times(2)).invoke("methodToMock", "parameter1");
}

但是,当我们希望模拟的私有(private)方法是静态的时,这种方法似乎不起作用。我希望创建以下类的部分模拟,并模拟 readFile 方法:

package org.rich.powermockexample;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

import static com.google.common.io.Files.readLines;

public class DataProvider {

public static List<String> getData() {
List<String> data = null;
try {
data = readFile();
} catch (IOException e) {
e.printStackTrace();
}
return data;
}

private static List<String> readFile() throws IOException {
File file = new File("/some/path/to/file");
List<String> lines = readLines(file, Charset.forName("utf-8"));
return lines;
}

}

请有人告诉我这是如何实现的吗?

最佳答案

在做了更多研究之后,似乎 PowerMockito.spy() 和 PowerMockito.doReturn() 是这里需要的:

package com.richashworth.powermockexample;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;


@RunWith(PowerMockRunner.class)
@PrepareForTest({DataProvider.class})
public class ResultsWriterTest {

private static List<String> mockData = new ArrayList<String>();
private ResultsWriter resultsWriter;

@BeforeClass
public static void setUpOnce() {
final String firstLine = "Line 1";
final String secondLine = "Line 2";
mockData.add(firstLine);
mockData.add(secondLine);
}

@Before
public void setUp() {
resultsWriter = new ResultsWriter();
}

@Test
public void testGetDataAsString() throws Exception {
PowerMockito.spy(DataProvider.class);
PowerMockito.doReturn(mockData).when(DataProvider.class, "readFile");

final String expectedData = "Line 1\nLine 2\n";
final String returnedString = resultsWriter.getDataAsString();

assertEquals(expectedData, returnedString);
}

}

有关更多详细信息和完整的代码 list ,请在此处查看我的博客文章:https://richashworth.com/post/turbocharge-your-mocking-framework-with-powermock/

关于java - 是否可以在 PowerMock 中对私有(private)静态方法使用部分模拟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9719919/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com