簡體   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