PowerMock测试

编程入门 行业动态 更新时间:2024-10-26 03:24:56
本文介绍了PowerMock测试-设置类的静态字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很难找到一种设置类的静态字段的方法.基本上是这样的:

I'm having difficulty finding a way to set a static field of a class. It's basically like this:

public class Foo{ // ... private static B b = null; }

其中B是另一个类.

除了使用setInternalStateFromContext()以外,PowerMock中还有其他方法可以执行此操作吗?使用上下文类方法似乎对设置一个字段有些过大.

Is there any way to do this in PowerMock other than with setInternalStateFromContext()? Using the context class method seems a bit of overkill for setting one field.

谢谢.

推荐答案

Whitebox.setInternalState(Foo.class, b);

只要设置了非空值和(如果只有一个字段为B类),

就可以工作.如果您不能依靠这种奢侈,则必须提供字段名称,并将null强制转换为要设置的类型.在这种情况下,您将需要编写如下内容:

Works as long as you set a non-null value, and if theres only one field with the class of B. If you can't rely on that luxury, you have to provide the field-name and cast the null to the type you want to set. In that case you would need to write something like this:

Whitebox.setInternalState( Foo.class, "b", (B)null );

更多推荐

PowerMock测试

本文发布于:2023-10-31 06:40:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545317.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   PowerMock

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!