繁体   English   中英

使用org.springframework.test.util.ReflectionTestUtils测试方法

[英]Test a method using the org.springframework.test.util.ReflectionTestUtils

我有一个要测试的类方法

public class EllaDtoConverter {


    private static void convertToIp( final IrisBo irisBo, EllaRequestDto request ) {

        if( nonNull( irisBo.getDevice() ) ) {
            request.setIp( irisBo.getDevice().getIpAddress() );
        }
    }
}

提供了我的测试

@Test
public void testConvertToIp() {

    assertNotNull( validIrisBo.getDevice() );
    assertNotNull( validIrisBo.getDevice().getIpAddress() );

    EllaRequestDto ellaRequestDto = new EllaRequestDto();

    ReflectionTestUtils.invokeMethod( ellaRequestDto, "setIp", validIrisBo.getDevice().getIpAddress() );
}

可以这样保留它还是可以进行测试呢?

如果为private方法编写单独的测试会增加代码的可信度,则不应将其视为private方法。 private方法是实现细节,应通过类的接口进行测试。

如果您不想提取此private方法以使其可见(例如,作为单独的类),则可以使用默认的可见性修饰符至少避免Java反射:

public class EllaDtoConverter {
    static void convertToIp( final IrisBo irisBo, EllaRequestDto request ) {
        ...
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM