Search code examples
javaspring-bootjunit5

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


I have two opposite methods that I have to test

First method

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

Second method

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

Here is the implementation

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);
    }

}

If I pass 'D' and 'P' this will cause throwFunctionalException(ERROR_34) and that is fine

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

The problem is the isRtcPortabilityToFiberSip() method is never reached because if I pass 'P' and 'D'

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

How to reach !isRtcPortabilityToFiberSip with JUnit 5?

I googled the problem without any solution.


Solution

  • You ought to have three different test functions:

        @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!
        }
    

    A good practice is to have one assertion per test (although this is flexible)

    Your code does not really have assertions (they could be added by having checkRenumberingValidation return a boolean value, instead of throwing an exception when objects are not equal), but the conditions are being checked adequately, just in a more roundabout way than usual.


    For more reading about testing, I highly recommend the JUnit FAQ
    (written when JUnit 4 was current, but nearly all concepts can be applied generally -- this is a classic)