簡體   English   中英

有沒有辦法在JUnit 5中測試兩種相反的方法?

[英]Is there a way to test two opposite methods in JUnit 5?

我有兩種相反的方法需要測試

第一種方法

private boolean isGeoOrtInboundPortabilityToNonGeoSipFiber(Character oldNdipAffectation,
      Character newNdipAffectation) {
    return isNdip09NatifVoipSip(newNdipAffectation)
        && isNdipGeoOperator(oldNdipAffectation);
}

第二種方法

private boolean isNonGeoSipOutboundPortabilityToGeoFiberOrt(Character oldNdipAffectation,
      Character newNdipAffectation) {
    return isNdipGeoOperator(newNdipAffectation)
        && isNdip09NatifVoipSip(oldNdipAffectation);
}

這是實現

protected void checkRenumberingValidation(Character oldNdipAffectation,
      NdipRenumberingRequest request, String basicatCode) {
    var oldNdipIndicatif = findIndicatifByNd(request.getOldNdip());
    var newNdipIndicatif = findIndicatifByNd(request.getNewNdip());

    if (!isClassicIpPortedNds(request)
        || !isGeoOrtInboundPortabilityToNonGeoSipFiber(oldNdipAffectation, newNdipAffectation)
        || !isNonGeoSipOutboundPortabilityToGeoFiberOrt(oldNdipAffectation, newNdipAffectation)
        || !isRtcPortabilityToFiberSip(newNdipIndicatif, oldNdipAffectation, newNdipAffectation,
        basicatCode)
        && (
        !Objects.equals(newNdipIndicatif.getZne().getCzne(), oldNdipIndicatif.getZne().getCzne())
            || !Objects.equals(newNdipIndicatif.getCsitugeo(), oldNdipIndicatif.getCsitugeo()))
    ) {
      throwFunctionalException(ERROR_34);
    }

}

如果我傳遞 'D' 和 'P' 這將導致 throwFunctionalException(ERROR_34) 並且這很好

!isGeoOrtInboundPortabilityToNonGeoSipFiber('D', 'P') // Throw exception which is expected

問題是永遠不會達到isRtcPortabilityToFiberSip()方法,因為如果我通過 'P' 和 'D'

!isGeoOrtInboundPortabilityToNonGeoSipFiber('P', 'D') // Pass
isNonGeoSipOutboundPortabilityToGeoFiberOrt('P', 'D') // Throw exception which is expected
!isRtcPortabilityToFiberSip(newNdipIndicatif, oldNdipAffectation, newNdipAffectation,
        basicatCode) // never reached

如何使用 JUnit 5 到達!isRtcPortabilityToFiberSip

我用谷歌搜索了這個問題,沒有任何解決方案。

您應該具有三種不同的測試功能:

    @Test
    public void testIsGeoOrtInboundPortabilityToNonGeoSipFiber() throws Exception
    {
        !isGeoOrtInboundPortabilityToNonGeoSipFiber('P', 'D') // Pass
    }

    @Test(expected = Exception.class)
    public void testIsNonGeoSipOutboundPortabilityToGeoFiberOrt() throws Exception
    {
        isNonGeoSipOutboundPortabilityToGeoFiberOrt('P', 'D') // Throw exception which is expected
    }

    @Test
    public void testIsRtcPortabilityToFiberSip() throws Exception
    {
        !isRtcPortabilityToFiberSip(newNdipIndicatif, oldNdipAffectation, newNdipAffectation,
        basicatCode) // never reached, but now it will be!
    }

一個好的做法是每個測試有一個斷言(盡管這是靈活的)

您的代碼實際上並沒有斷言(可以通過讓checkRenumberingValidation返回一個 boolean 值來添加它們,而不是在對象不相等時拋出異常),但是條件正在被充分檢查,只是以比平常更迂回的方式。


有關測試的更多閱讀,我強烈推薦JUnit FAQ
(JUnit 4 時寫的,但是幾乎所有的概念都可以通用——這是經典)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM