Test a private static method with the PowerMock

Keywords: java mockito powermock

Question: 

I have a POJO class provided below,

@JsonInclude( JsonInclude.Include.NON_NULL )
@Getter
@Setter
public class EllaRequestDto extends BaseDto {

    private String trackingId;

    @NotNull
    private String email;

    private String ip;

    private String firstName;

    private String lastName;

    private AddressDto billingAddress;

    private AddressDto shippingAddress;

    private TransactionDto transaction;

    private ServiceDto service;
}

I also write a converter provided below,

public final class EllaDtoConverter {

    private EllaDtoConverter() {
        throw new PrivateConstructorException();
    }

    /**
     * Map IrisBo attributes to Ella-RequestDTO.
     *
     * @param irisBo irisEo object
     * @return RequestDto ella requestDTO
     */
    public static EllaRequestDto convertToRequest( final IrisBo irisBo ) throws IllegalArgumentException {

        notNull( irisBo, "Bad argument: IrisBo resolved to null" );
        notNull( irisBo.getBuyer().getEmail(), "Bad argument: Email Address resolved to null" );

        final AddressDto shipAddressDto = convertToShipAddressDto( irisBo );
        final AddressDto billAddressDto = convertToBillAddressDto( irisBo );
        final ServiceDto serviceDto = convertToServiceDto( irisBo );
        final TransactionDto transactionDto = convertToTransactionDto( irisBo );

        final EllaRequestDto request = new EllaRequestDto();

        request.setTrackingId( irisBo.getRequestInfo().getTransactionId() );
        request.setEmail( irisBo.getBuyer().getEmail() );

        convertToIp( irisBo, request );
        convertToNames( irisBo, request );

        request.setBillingAddress( shipAddressDto );
        request.setShippingAddress( billAddressDto );
        request.setTransaction( transactionDto );
        request.setService( serviceDto );

        return request;
    }

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

        if( !isNull( irisBo.getBuyer() ) ) {
            request.setLastName( irisBo.getBuyer().getLastName() );
            request.setFirstName( irisBo.getBuyer().getFirstName() );
        }
    }


    // ....... some code 
} 

My test class is here,

@RunWith( MockitoJUnitRunner.class )
public class EllaDtoConverterTest {

    @Mock
    private EllaRequestDto ellaRequestDto;

    private IrisBo validIrisBo;

    private static final double DELTA = 1e-15;

    private String validTrackingId;
    private String validEmailAddress;
    private String validIpAddress;
    private String validFirstName;
    private String validLastName;

    @Rule
    public ExpectedException expectedException = ExpectedException.none();

    @Before
    public void setup() {

        validIrisBo = EllaTestDataProvider.createValidIrisBoWithoutRequest();

        validTrackingId = "valid-trx-id";
        validEmailAddress = "test@test.com";
        validIpAddress = "123.120.12.12";
        validFirstName = "Max";
        validLastName = "Musterman";
    }


     // .. some code 

 } 

I am able to do the null check for the irisBo.getBuyer() inside the EllaDtoConverter::convertToNames method. How do I test the request.setFirstName and request.setLastName calls inside?

Answers: