使用返回整数列表的电源模拟测试私有方法

发布于 2024-11-30 02:45:36 字数 83 浏览 2 评论 0原文

我有一个私有方法,它接受整数值列表,返回整数值列表。我如何使用电源模拟来测试它。我是 powermock 的新手。我可以用简单的模拟进行测试吗?如何..

I have a private method which take a list of integer value returns me a list of integer value. How can i use power mock to test it. I am new to powermock.Can i do the test with easy mock..? how..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷夜 2024-12-07 02:45:36

来自文档,在名为“常见 - 绕过封装”的部分中:

使用Whitebox.invokeMethod(..)调用一个私有方法
实例或类。

您还可以在同一部分中找到示例。

From the documentation, in the section called "Common - Bypass encapsulation":

Use Whitebox.invokeMethod(..) to invoke a private method of an
instance or class.

You can also find examples in the same section.

若无相欠,怎会相见 2024-12-07 02:45:36

这是如何执行此操作的完整示例:

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

import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

class TestClass {
    private List<Integer> methodCall(int num) {
        System.out.println("Call methodCall num: " + num);
        List<Integer> result = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            result.add(new Integer(i));
        }
        return result;
    }
}

 @Test
 public void testPrivateMethodCall() throws Exception {
     int n = 10;
     List result = Whitebox.invokeMethod(new TestClass(), "methodCall", n);
     Assert.assertEquals(n, result.size());
 }

Here is a full example how to do to it:

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

import org.junit.Assert;
import org.junit.Test;
import org.powermock.reflect.Whitebox;

class TestClass {
    private List<Integer> methodCall(int num) {
        System.out.println("Call methodCall num: " + num);
        List<Integer> result = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            result.add(new Integer(i));
        }
        return result;
    }
}

 @Test
 public void testPrivateMethodCall() throws Exception {
     int n = 10;
     List result = Whitebox.invokeMethod(new TestClass(), "methodCall", n);
     Assert.assertEquals(n, result.size());
 }
狼性发作 2024-12-07 02:45:36
Whitebox.invokeMethod(myClassToBeTestedInstance, "theMethodToTest", expectedFooValue);
Whitebox.invokeMethod(myClassToBeTestedInstance, "theMethodToTest", expectedFooValue);
黯然 2024-12-07 02:45:36

当您想使用 Powermockito 测试私有方法并且该私有方法具有语法:

private int/void testmeMethod(CustomClass[] params){
....
}

在您的测试类方法中:

CustomClass[] params= new CustomClass[] {...}
WhiteboxImpl.invokeMethod(spy,"testmeMethod",params)

由于参数而无法工作。您收到一条错误消息,表明带有该参数的 testmeMethod 不存在
看这里:

WhiteboxImpl 类

public static synchronized <T> T invokeMethod(Object tested, String methodToExecute, Object... arguments)
            throws Exception {
        return (T) doInvokeMethod(tested, null, methodToExecute, arguments);
    }

对于 Array 类型的参数,PowerMock 很混乱。因此,在您的测试方法中将其修改为:

WhiteboxImpl.invokeMethod(spy,"testmeMethod",(Object) params)

对于无参数私有方法,您不会遇到此问题。我记得它适用于 Primitve 类型和包装类的参数。

“理解 TDD 就是理解软件工程”

When you want to test a private method with Powermockito and this private method has syntax:

private int/void testmeMethod(CustomClass[] params){
....
}

in your testing class method:

CustomClass[] params= new CustomClass[] {...}
WhiteboxImpl.invokeMethod(spy,"testmeMethod",params)

will not work because of params. you get an error message that testmeMethod with that arguments doesn't exist
Look here:

WhiteboxImpl class

public static synchronized <T> T invokeMethod(Object tested, String methodToExecute, Object... arguments)
            throws Exception {
        return (T) doInvokeMethod(tested, null, methodToExecute, arguments);
    }

For arguments of type Array, PowerMock is messed up. So modify this in your test method to:

WhiteboxImpl.invokeMethod(spy,"testmeMethod",(Object) params)

You don't have this problem for parameterless private methods. As I can remember it works for parameters of type Primitve type and wrapper class.

"Understanding TDD is understanding Software Engineering"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文