[英]Test a method using the org.springframework.test.util.ReflectionTestUtils
I have a class method that I would like test 我有一个要测试的类方法
public class EllaDtoConverter {
private static void convertToIp( final IrisBo irisBo, EllaRequestDto request ) {
if( nonNull( irisBo.getDevice() ) ) {
request.setIp( irisBo.getDevice().getIpAddress() );
}
}
}
My test is provided 提供了我的测试
@Test
public void testConvertToIp() {
assertNotNull( validIrisBo.getDevice() );
assertNotNull( validIrisBo.getDevice().getIpAddress() );
EllaRequestDto ellaRequestDto = new EllaRequestDto();
ReflectionTestUtils.invokeMethod( ellaRequestDto, "setIp", validIrisBo.getDevice().getIpAddress() );
}
Does it okay to leave it like this or I have a better option to test? 可以这样保留它还是可以进行测试呢?
If writing a separate test for private
method increases the confidence in the code than it should not be a private
method. 如果为
private
方法编写单独的测试会增加代码的可信度,则不应将其视为private
方法。 private
methods are implementation details and should be tested through the interface of the class. private
方法是实现细节,应通过类的接口进行测试。
In case you don't want to extract this private
method to make it visible eg as a separate class, you can use the default visibility modifier to at least avoid Java reflection: 如果您不想提取此
private
方法以使其可见(例如,作为单独的类),则可以使用默认的可见性修饰符至少避免Java反射:
public class EllaDtoConverter {
static void convertToIp( final IrisBo irisBo, EllaRequestDto request ) {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.