Java 在 Junit 测试中使用 ReflectionTestUtils.setField()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44667573/
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
Use of ReflectionTestUtils.setField() in Junit testing
提问by
I am new in JUnittesting so i have a question. Can anyone please tell me why we use ReflectionTestUtils.setField()
in our Junit testing with example.
我是 JUnittesting 的新手,所以我有一个问题。谁能告诉我为什么我们ReflectionTestUtils.setField()
在我们的 Junit 测试中使用示例。
回答by Patrick
Like mentioned in the comment, the java docs explains the usage very well. But I want to give you also a simple example.
就像评论中提到的那样,java 文档很好地解释了用法。但我也想给你举一个简单的例子。
Lets say you have an Entity class with private or protected field access and no provided setter method.
假设您有一个具有私有或受保护字段访问权限的实体类,并且没有提供 setter 方法。
@Entity
public class MyEntity {
@Id
private Long id;
public Long getId(Long id){
this.id = id;
}
}
In you test class you are not able to set an id
of your entity
because of the missing setter method.
在您的测试类中id
,entity
由于缺少 setter 方法,您无法设置一个。
Using ReflectionTestUtils.setField
you are able to do that for testing purpose:
使用ReflectionTestUtils.setField
您可以将其用于测试目的:
ReflectionTestUtils.setField(myEntity, "id", 1);
The Parameters are described:
参数说明:
public static void setField(Object targetObject,
String name,
Object value)
Set the field with the given name on the provided targetObject to the supplied value.
This method delegates to setField(Object, String, Object, Class), supplying null for the type argument.
Parameters:
targetObject - the target object on which to set the field; never null
name - the name of the field to set; never null
value - the value to set
But give it a try and read the docs.
但是试一试并阅读文档。
回答by brice jiang
it's very useful when we want to write unit test, such as:
当我们要编写单元测试时它非常有用,例如:
class A{
int getValue();
}
class B{
A a;
int caculate(){
...
int v = a.getValue();
....
}
}
class ServiceTest{
@Test
public void caculateTest(){
B serviceB = new B();
A serviceA = Mockito.mock(A.class);
Mockito.when(serviceA.getValue()).thenReturn(5);
ReflectionTestUtils.setField(serviceB, "a", serviceA);
}
}
回答by Pramod S. Nikam
One more Use Case:
另一个用例:
We externalised many properties Such as: URL's , endpoint and many other properties in application properties like below:
我们外部化了许多属性,例如: URL 、端点和应用程序属性中的许多其他属性,如下所示:
kf.get.profile.endpoint=/profile
kf.get.clients.endpoint=clients
and then use it in application like below:
然后在应用程序中使用它,如下所示:
@Value("${kf.get.clients.endpoint}")
private String getClientEndpoint
and whenever when we write unit test , we get NullPointerExceptionbecause Spring cannot inject @value similarly as @Autowired does. ( at least at the moment , I dont know alternative. ) so to avoid that we can use ReflectionTestUtilsto inject externalised properties. like below:
每当我们编写单元测试时,我们都会得到NullPointerException,因为 Spring 不能像 @Autowired 那样注入 @value。(至少目前,我不知道替代方案。)所以为了避免我们可以使用ReflectionTestUtils来注入外化属性。像下面这样:
ReflectionTestUtils.setField(targetObject,"getClientEndpoint","lorem");