0

我正在尝试模拟一个内部调用私有方法的方法。此私有方法使用方法读取文件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) ArrayList testList = new ArrayList(); testList.add("00"); TestLoad 实例 = PowerMock.createPartialMock(TestLoad.class,"populateList"); PowerMock.expectPrivate(instance, "populateList").andReturn(testList); PowerMock.replay(实例);

以下是日志供参考:

java.lang.IllegalStateException:无法转换名称为 com.xyz.TestLoad 的类。原因:java.io.IOException:无效常量类型:org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:266) at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java) 17 :180) at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at 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。

4

2 回答 2

0

您的测试代码看起来像是在使用 PowerMock EasyMock。但是你的帖子也被标记为 mockito 和 powermockito。

我正在使用 powermockito 提供以下解决方案。

主要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]
于 2015-10-21T04:35:04.473 回答
0

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

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

**

于 2015-10-21T21:51:16.203 回答