元类在测试过程中没有替换实例方法?

时间:2019-03-27 07:55:46

标签: grails groovy grails-2.2

这是我准备的示例测试,用于检查元类方法替换是否有效。下面是被测方法。此方法位于PaymentController内部。

请看一行

CreateTransactionResponse tresponse = controller.getApiResponse();
代码中的

。在测试代​​码中,我正在使用元类来替换getApiResponse()方法。

def justTest(){

    def descriptor = params['dataDescriptor']
    def value = params['dataValue']

    String firstName = params[AuthNetField.X_FIRST_NAME.fieldName]
    String lastName = params[AuthNetField.X_LAST_NAME.fieldName]
    String address = params[AuthNetField.X_ADDRESS.fieldName]
    String city = params[AuthNetField.X_CITY.fieldName]
    String state = params[AuthNetField.X_STATE.fieldName]
    String zip = params[AuthNetField.X_ZIP.fieldName]
    String country = params[AuthNetField.X_COUNTRY.fieldName]
    String phone = params[AuthNetField.X_PHONE.fieldName]
    String email = params[AuthNetField.X_EMAIL.fieldName]

    String apiLoginId = grailsApplication.config.net.authorize.apiLoginId


    if (grailsApplication.config.net.authorize.environment == net.authorize.Environment.PRODUCTION){
        ApiOperationBase.setEnvironment(Environment.PRODUCTION);
    }
    else{
        ApiOperationBase.setEnvironment(Environment.SANDBOX);
    }

    String transactionKey = grailsApplication.config.net.authorize.transactionKey


    MerchantAuthenticationType merchantAuthenticationType  = new MerchantAuthenticationType() ;
    merchantAuthenticationType.setName(apiLoginId);
    merchantAuthenticationType.setTransactionKey(transactionKey);
    ApiOperationBase.setMerchantAuthentication(merchantAuthenticationType);

    // Populate the payment data
    PaymentType paymentType = new PaymentType();
    OpaqueDataType OpaqueData = new OpaqueDataType();
    OpaqueData.setDataDescriptor(descriptor);
    OpaqueData.setDataValue(value);
    paymentType.setOpaqueData(OpaqueData);


    CustomerDataType cdt = new CustomerDataType()
    cdt.setEmail(email)


    // Create the payment transaction request
    TransactionRequestType txnRequest = new TransactionRequestType();
    txnRequest.setTransactionType(TransactionTypeEnum.AUTH_CAPTURE_TRANSACTION.value());
    txnRequest.setPayment(paymentType);

    txnRequest.setCustomer(cdt)


    txnRequest.setAmount(5);


    // Make the API Request
    CreateTransactionRequest apiRequest = new CreateTransactionRequest();
    apiRequest.setTransactionRequest(txnRequest);
    CreateTransactionController controller = new CreateTransactionController(apiRequest);
    controller.execute();


    CreateTransactionResponse tresponse = controller.getApiResponse();


    render "hello"


}

这是集成测试,其中我替换了CreateTransactionController类的getApiResponse()方法。当测试运行时,则不会发生此替换,即它不会打印“在这里...”。因此,我感谢任何有关为何无法使用此替代品的指南。感谢您的帮助。谢谢!

@Test
 void test(){

    PaymentController pc = new PaymentController()


    CreateTransactionController.metaClass.getApiResponse = {
        println "got here..."
    }


    pc.justTest()


 }

更新:

这很奇怪。

我将justTest更改为另一个authorize.net测试代码。

def justTest(){


    String apiLoginId = grailsApplication.config.net.authorize.apiLoginId
    String transactionKey = grailsApplication.config.net.authorize.transactionKey

    def voidTransactionResponse = new TransactionResponse()
    net.authorize.aim.Result<net.authorize.aim.Transaction> voidResult

    Merchant merchant = Merchant.createMerchant(grailsApplication.config.net.authorize.environment, apiLoginId, transactionKey)
    net.authorize.aim.Transaction transaction = merchant.createAIMTransaction(net.authorize.TransactionType.VOID, new BigDecimal(2))
    transaction.transactionId = "324234234"
    voidResult = merchant.postTransaction(transaction)


    render "hello"

}

这是测试代码的元替换。

PaymentController pc = new PaymentController()
Merchant.metaClass.postTransaction = { net.authorize.Transaction transaction ->
        println "asd"
    }    
pc.justTest()

在这里有效。即执行println“ asd”。换句话说,替换了Merchant的postTransaction方法。所以我想知道为什么替换在Merchant类而不是CreateTransactionController类上起作用。我感谢任何见解。谢谢!

UPDATE2: 我要记录的一件事是authorize.net库以jar格式下载并添加到lib文件夹中。因此无法从BuildConfig文件中解析该库。这是因为使用的grails框架是旧的2.2,而库是新的,因此由于它不在存储库中,因此必须将其下载为jar并添加到lib文件夹中。这会破坏元替换吗?

0 个答案:

没有答案