简体   繁体   English

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

[英]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.

相关问题 为什么我的项目找不到org.springframework.test或org.springframework.boot.test? - Why can't my project locate org.springframework.test or org.springframework.boot.test? org.springframework.boot.test.mock.mockito.MockReset上的NoClassDefFoundError - NoClassDefFoundError on org.springframework.boot.test.mock.mockito.MockReset MiniKdc 无法从 org.springframework.security.kerberos.test.MiniKdc 获得 - MiniKdc not available from org.springframework.security.kerberos.test.MiniKdc 无法解析导入org.springframework.test.context.junit4.SpringRunner - The import org.springframework.test.context.junit4.SpringRunner cannot be resolved 在Eclipse中测试NG测试失败,并显示错误NoClassDefFoundError:org / springframework / test / context / testng / AbstractTestNGSpringContextTests - Test NG test in eclipse fails with the error NoClassDefFoundError: org/springframework/test/context/testng/AbstractTestNGSpringContextTests org.springframework.test也无法解决,同时存在maven依赖项 - org.springframework.test cannot be resolved also with maven dependency present 无法解析符号org.springframework.test.web.server - Cannot resolve symbol org.springframework.test.web.server java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils - java.lang.NoClassDefFoundError: org/springframework/test/context/TestContextAnnotationUtils package org.springframework.boot.test 不存在 - package org.springframework.boot.test does not exist 如何单独测试Util类依赖于其他Util类方法? - How to Unit test Util class with dependency on other Util class method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM