使用PowerMock测试私有静态方法

时间:2019-06-03 16:27:05

标签: java mockito powermock

我下面有一个POJO类,

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

我还写了下面提供的转换器

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 
} 

我的测试班在这里

@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 

 } 

我能够在null方法内对irisBo.getBuyer()进行EllaDtoConverter::convertToNames检查。如何测试内部的request.setFirstNamerequest.setLastName呼叫?

0 个答案:

没有答案