java 访问用于 junit 测试的私有字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27857612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access a private field for a junit test
提问by schmimona
I am trying to initialize a private field from a class in order to unit test its methods. For that I am using reflection but I am always getting an IllegalArgumentException and I don't understand what I am doing wrong.
我试图从一个类初始化一个私有字段,以便对其方法进行单元测试。为此,我正在使用反射,但我总是收到 IllegalArgumentException 并且我不明白我做错了什么。
My code looks something like this:
我的代码看起来像这样:
public class MyClass {
private BufferedReader reader;
public void methodToTest(){
doSomethingwith(reader);
}
}
public class testClass{
@Test
public void testMethod(){
Field reader = MyClass.class.getDeclaredField("reader");
reader.setAccessible(true);
StringReader stringReader = new StringReader("some string");
BufferedReader readerToSet = new BufferedReader(stringReader);
reader.set(readerToSet, readerToSet);
MyClass instance = new MyClass();
instance.methodToTest();
}
}
I get this error when I am trying to run the test:
当我尝试运行测试时出现此错误:
Can not set java.io.BufferedReader field MyClass.receiveReader to java.io.BufferedReader
I also tried getting the value of the field from the class and setting to the reader. But the value returns null and I still get the same error message.
我还尝试从类中获取字段的值并将其设置给读者。但是该值返回 null 并且我仍然收到相同的错误消息。
Any idea how I could initialize the field so I can test the method?
知道如何初始化该字段以便我可以测试该方法吗?
回答by laune
You must instantiate the object beforeyou can set its field! (And you have misspelled the BufferedReader reference.)
您必须先实例化对象,然后才能设置其字段!(并且您拼错了 BufferedReader 参考。)
Field reader = MyClass.class.getDeclaredField("reader");
reader.setAccessible(true);
MyClass mc = new MyClass();
StringReader stringReader = new StringReader("some string");
BufferedReader readerToSet = new BufferedReader(stringReader);
reader.set(mc, readerToSet);
mc.methodToTest();
回答by Marcelo Xavier
You can use Mockito for that (org.mockito.internal.util.reflection.Whitebox)
你可以使用 Mockito (org.mockito.internal.util.reflection.Whitebox)
Whitebox.setInternalState(object, "fieldName", fieldValue);
回答by pbespechnyi
Answer by @kocko is correct for most cases. But if you do not want to show implementation details, you can just do not set access level modifier to your field (default
access level).
@kocko 的回答在大多数情况下是正确的。但是如果你不想显示实现细节,你可以不为你的字段设置访问级别修饰符(default
访问级别)。
So in your case field declaration should look like this:
因此,在您的情况下,字段声明应如下所示:
public class MyClass {
BufferedReader reader;
...
}
And then make sure your test is located in same package with your code, but in test
directory. If all is done right, you will be able to access it with out any hacks.
然后确保您的测试与您的代码位于同一个包中,但在test
目录中。如果一切顺利,您将无需任何黑客即可访问它。
Note: default
access level means that the thing is visible only inside class and package.
注意:default
访问级别意味着该事物仅在类和包内可见。