使用Mockito从void函数中获取局部变量的值

发布于 2025-01-26 10:38:41 字数 238 浏览 3 评论 0原文

我有一个类似的方法:

public void process {
   ArrayList<String> data = new ArrayList<>();
   ...
   ...
}

data是一个本地变量,在调用myobject.process()中,我要在测试功能中访问它的值。我有什么办法可以做到吗?

I have a method like below:

public void process {
   ArrayList<String> data = new ArrayList<>();
   ...
   ...
}

data is a local variable, the value of which I want to access after calling myObject.process() inside the test function. Is there any way I can do that?

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

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

发布评论

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

评论(1

囍笑 2025-02-02 10:38:41

Mockito无法访问由您的代码创建的本地变量。测试代码的唯一方法是检查该变量是否正在实现其他事物并验证该更改。这个答案应该可以帮助您。
https://stackoverflow.com/a/a/30188745/1705205​​1

Mockito cannot access a local variable which is created by your code. The only way to test the code is to check whether something else is being effected by that variable and verify that change. This answer should help you out.
https://stackoverflow.com/a/30188745/17052051

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