模拟私有方法



我试图模拟一个内部调用私有方法的方法。这个私有方法使用classloader.getclass方法读取一个文件,并填充一个列表,该列表是类的静态变量。我尝试了所有可能的方法来模拟这个私有方法,但它似乎不起作用。

class TestLoad {
    private static List <String> myList = new ArrayList <String> ();
    private static final String filename = "/myfile.txt";
    public XYZ generateList(Abc abc) throws Exception {
        populateList();
    }
    private populateList() {
        ClassLoader classLoader = TestLoad.class.getClassLoader();
        File listfile = new File(classLoader.getResource(
        filename).getFile());
        List <String> localList = new ArrayList <String> ();
        try (Scanner scanner = new Scanner(listfile)) {
            while (scanner.hasNextLine()) {
                String text = scanner.nextLine();
                localList.add(text.trim());
            }
            scanner.close();
        } catch (IOException e) {}
        return localList;
    }
}

我试图模拟populateList(),但一直控制进入方法。我尝试了几乎所有的选项在网上的mockito和powermock,但它似乎不工作。我的Junit如下

类开始有@RunWith(PowerMockRunner.class)@PrepareForTest (TestLoad.class)testList = new ArrayList();testList.add("00");TestLoad instance = PowerMock.createPartialMock(TestLoad.class,"populateList");PowerMock。expectPrivate(例如,"populateList")。andReturn ( testList);PowerMock.replay(实例);

下面是日志供参考:

. lang。转换名为com.xyz.TestLoad的类失败。原因:java.io.IOException: invalid constant type: 18 at 17org.powermock.core.classloader.MockClassLoader.loadMockClass (MockClassLoader.java: 266)org.powermock.core.classloader.MockClassLoader.loadModifiedClass (MockClassLoader.java: 180)org.powermock.core.classloader.DeferSupportingClassLoader.loadClass (DeferSupportingClassLoader.java: 68)java.lang.ClassLoader.loadClass (ClassLoader.java: 357). lang . class。forName0(本地方法)java.lang.Class.forName (Class.java: 348)org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader (JUnit4TestSuiteChunkerImpl.java: 145)org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader (JUnit4TestSuiteChunkerImpl.java: 40)org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators (AbstractTestSuiteChunkerImpl.java: 244)在org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl。(JUnit4TestSuiteChunkerImpl.java: 61)在org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner。(AbstractCommonPowerMockRunner.java: 32)在org.powermock.modules.junit4.PowerMockRunner。(PowerMockRunner.java: 34)在sun.reflect.NativeConstructorAccessorImpl。newInstance0(本地方法)

您的测试代码看起来像是在使用PowerMock EasyMock。但是你的帖子也被贴上了mockito &powermockito .

我用powermockit给出了下面的解决方案。

Java主类:

public class TestLoad {
private static List<String> myList = new ArrayList<String>();
private static final String filename = "/myfile.txt";
public List<String> generateList(Abc abc) throws Exception {
    System.out.println("generateList method is called");
    return populateList();
}
private List<String> populateList() {
    System.out.println("populateList method is called");
    ClassLoader classLoader = TestLoad.class.getClassLoader();
    File listfile = new File(classLoader.getResource(filename).getFile());
    // Read listfile and build localList logic here
    List<String> localList = new ArrayList<String>();
    return localList;
 }
}

测试Java类

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
@PrepareForTest(TestLoad.class)
@RunWith(PowerMockRunner.class)
public class TestLoadTest {
@Test
public void testPrivateMethod() throws Exception{
    List<String> testList = new ArrayList<String>();
    testList.add("00");
    TestLoad mockInstance = PowerMockito.spy(new TestLoad());
    PowerMockito.doReturn(testList).when(mockInstance, "populateList");
    List<String> outputList = mockInstance.generateList(new Abc());
    System.out.println("test output:" + outputList);
    PowerMockito.verifyPrivate(mockInstance, Mockito.times(1)).invoke("populateList"); 
}
}

控制台输出:

generateList method is called
test output:[00]

经过大量研究,我找到了解决方案。我们需要在pom.xml中添加以下依赖项以防止出现此错误…* *

<dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.2-GA</version>
        </dependency>
* *

相关内容

  • 没有找到相关文章

最新更新