kotlin调用类中的方法_一种轻松的方法来测试Kotlin中令人沮丧的静态方法调用

kotlin调用类中的方法

by Oleksii Fedorov

通过Oleksii Fedorov

一种轻松的方法来测试Kotlin中令人沮丧的静态方法调用 (A stress-free way to test frustrating static method calls in Kotlin)

Let me make a wild guess… You have encountered some code in Kotlin that is using some third-party library. The API that the library provides is one or a few static methods. And you want to test some code using these static methods. It is painful.

让我大胆地猜测一下……您在Kotlin中遇到了一些使用某些第三方库的代码。 该库提供的API是一种或几种静态方法。 您想使用这些静态方法测试一些代码。 真痛苦

You are not sure how to approach that problem.

您不确定如何解决该问题。

Perhaps you ask yourself, “When will third-party library authors stop using static methods?”

也许您问自己:“第三方库作者何时会停止使用静态方法?”

Anyway, who am I to tell you how to test static method calls in Kotlin?

无论如何,我该告诉谁如何在Kotlin中测试静态方法调用?

I’m a fanatic of testing and test-driven development evangelist for the last five years — they call me TDD Fellow for a reason. I have been working with Kotlin in production for about two years at the time of writing this.

在过去的五年中,我热衷于测试和测试驱动的开发宣传人员-他们之所以称呼我为TDD研究员 ,是有原因的。 在撰写本文时,我已经在Kotlin的生产环境中工作了大约两年。

Onward!

向前!

That is how I feel when I see such awful APIs:

当我看到如此糟糕的API时,就是这种感觉:

Let me show you what I mean with a rough example that I have been dealing with recently. The library was a newrelic client. To use it I had to call a static method on some class. If simplified, it looks something like this:

让我通过最近处理的一个粗糙示例向您展示我的意思。 该图书馆是newrelic客户。 要使用它,我必须在某个类上调用静态方法。 如果简化,它看起来像这样:

NewRelicClient.addAttributesToCurrentRequest(“orderId”, order.id)

I needed to change what exactly we are sending, and I had to add more attributes. Since I wanted to have confidence that my change is not breaking anything and does exactly the thing I want, I needed to write a test. There was no test for this code yet.

我需要更改发送的确切内容,并且必须添加更多属性。 由于我想确信自己所做的更改不会破坏任何东西,并且完全可以完成我想要的事情,因此我需要编写测试。 此代码尚未测试。

If you are still reading, I’m assuming you are in the same situation. Or you have been in the past.

如果您仍在阅读,我假设您处于相同的情况。 或者您曾经去过。

I agree that is a painful situation.

我同意这是一个痛苦的情况。

How am I supposed to mock these calls in the test?

我应该如何在测试中模拟这些电话?

I know, it is frustrating that most of the mocking libraries are unable to mock static method calls. And even the ones that work in Java don’t always work in Kotlin.

我知道,令人沮丧的是,大多数模拟库无法模拟静态方法调用。 甚至那些在Java中工作的工具也不一定总是在Kotlin中工作。

There are libraries that could do that, such as powermock, for instance. But you know what? Perhaps, you are already using mockito or some other library. Adding another mocking tool to the project will make things more confusing and frustrating.

有一些库可以做到这一点,例如powermock, 。 但是你知道吗? 也许,您已经在使用mockito或其他库。 向项目添加另一个模拟工具会使事情变得更加混乱和令人沮丧。

I know how annoying it is to have multiple tools for the same job in the same codebase. That causes a hell lot of confusion for everyone.

我知道在同一代码库中为同一工作使用多个工具是多么烦人。 这给每个人带来了很多混乱。

Well, that problem was already solved about two decades ago!

好吧,这个问题已经在大约二十年前解决了!

Interested? Come for a ride.

有兴趣吗 过来兜风。

向谦虚对象重构 (Refactoring towards the Humble Object)

Let’s take a look at the code that we are working with here:

让我们看一下我们在这里使用的代码:

class FulfilOrderService {

    fun fulfil(order: Order) {
    
        // .. do various things ..
        
        NewRelicClient.addAttributesToCurrentRequest(
                "orderId", order.id)
        NewRelicClient.addAttributesToCurrentRequest(
                "orderAmount", order.amount.toString())
                
    }
    
}

It is doing various things with the order to fulfill it, and then it is assigning a few attributes to the current request for newrelic.

它按照顺序执行各种操作,然后为当前请求newrelic分配一些属性。

The first thing that we will do together here is extract the method addAttributesToRequest. We also want to parametrize it with key and value arguments. You can do so manually, or, if you are lucky enough to use IntelliJ IDEA, you can do such refactoring automatically.

我们将在这里一起做的第一件事是提取方法addAttributesToRequest 。 我们还希望使用keyvalue参数对其进行参数化。 您可以手动执行此操作,或者,如果有幸使用IntelliJ IDEA,则可以自动执行此类重构。

Here is how:

方法如下:

  1. Select ”orderId” and extract a local variable. Name it key.

    选择”orderId”并提取局部变量。 将其命名为key

  2. Select order.id and extract a local variable. Name it value.

    选择order.id并提取局部变量。 将其命名为value

  3. Select NewRelicClient.addAttribute

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值