[英]Calling a private static void method
I have a private static void
method namely convertToIp
in the class EllaDtoConverter
that has a private constructor. 我有一个
private static void
方法,即类EllaDtoConverter
中的convertToIp
,它有一个私有构造函数。 If I try to create an instance of the class, it throws an exception. 如果我尝试创建该类的实例,它会抛出异常。 The code is provided,
代码是提供的,
public final class EllaDtoConverter {
private EllaDtoConverter() {
throw new PrivateConstructorException();
}
private static void convertToIp( final IrisBo irisBo, EllaRequestDto request ) {
if( !isNull( irisBo.getDevice() ) ) {
request.setIp( irisBo.getDevice().getIpAddress() );
}
}
// ..... some code
}
I can create the instances of the IrisBo
and the EllaRequestDto
and pass inside the method. 我可以创建
IrisBo
和EllaRequestDto
的实例并传递给方法。 Do I have an option to call the convertToIp
method outside from another class (even with using the reflection)? 我是否可以选择从另一个类调用
convertToIp
方法(即使使用反射)?
From the junit
tag I assume you want to know if it is possible to test the method. 从
junit
标签我假设您想知道是否可以测试该方法。
I suggest you this article which basically says that you shouldn't be testing private methods (static or not doesn't really matter) on their own. 我建议你这篇文章基本上说你不应该自己测试私有方法(静态或不静态)。
The main idea is that unit testing should test the contract that your module is exposing, assuring that whoever calls you get what it expects from you, and private methods are not part of the contract. 主要想法是单元测试应该测试您的模块正在暴露的合同,确保任何调用您的人都能得到您期望的内容,并且私有方法不是合同的一部分。
As @bracco23 written before, you should read an article about testing private methods. 正如之前写的@bracco23,您应该阅读一篇关于测试私有方法的文章。 However, if you have already decided to do it (despiting all this negative aspects which comes with testing them), there is a special framework called PowerMockito .
但是,如果您已经决定这样做(鄙视测试它们所带来的所有这些消极方面),那么有一个名为PowerMockito的特殊框架。 PowerMockito allows us to test private or static methods.
PowerMockito允许我们测试私有或静态方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.