How to test methods with nullable parameters in Kotlin

Background:

I had a Groovy class which has a method that relies on an another Kotlin method imported from a dependency as the following:

class DependencyClass {
    fun remoteMethod(param1: String, param2: Int, param3: Boolean): String {
        // Method logic
        return "result"
    }

}
// Groovy class
class MyClass {

    // Google guice injection
    @Inject
    private DependencyClass depClassObj

    void myMethod(input) {

        // Call with 3 parameters
        def result = depClassObj.remoteMethod("example", 42, true)
        // Other logic...
    }
}

The tests of my Groovy class methods are written in Kotlin as the following:

    @Test
    fun `test myMethod which calls anotherMethod in DependencyClass`() {

        // Mock & bind etc..
        val depClassObjMock = mock<DependencyClass>()

        // Step 3: Define behavior for myMethod using matchers
        whenever(depClassObjMock.remoteMethod( any(), any(), any() )).thenThrow(NullPointerException)

        myClassMock.myMethod()
        
        // TestUtil class which runs the methods...
        assertThrows<NullPointerException>(flowRunner.runFlow("myClassMock.myMethod"))

}
 

Problem:

The Dependency that I was relying on got updated, as the remoteMethod() started to take 4 parameters instead of 3, where the 4th parameter is nullable as the following:


class DependencyClass {
    fun remoteMethod(param1: String, param2: Int, param3: Boolean, param4: String? = null): String {
        // Method logic
        return "result"
    }

}

Now all my tests started to fail due to an error that looks:

Invalid use of argument matchers!
4 matchers expected, 3 recorded:...

What have I tried:

First try (didn’t work)

I tried editing the tests by passing a new matcher for the new parameter as anyOrNull() or eq(Null) or isNull()` as the follwing:

whenever(depClassObjMock.myMethod( any(), any(), any(), anyOrNull() )).thenThrow(NullPointerException)

but now the whenever or when statement doesn’t match the myMethod schema causing the tests to do the actual logic of the myMethod (without mocking).

as

depClassObjMock.remoteMethod(any(), any(), any(), anyOrNull()) // takes 4 parameters
!==
depClassObj.remoteMethod("example", 42, true) // takes 3 parameters, even if the 4th is nullable it's not pointing for the whenever statement with the 4 parameters.

Second try (worked)

I had to edit the logic inside `myMethod() to make the tests work (which is not a good practice as I recall) as the following:


def result = depClassObj.remoteMethod("example", 42, true, null)

Ask:

  1. Is there any way to test kotlin methods with nullable parameters without editing the actual logic (only edit the tests) ?

  2. Does this issue has anything to do with the fact that we are calling a Kotlin method from a Groovy method then testing the groovy flow in Kotlin tests ?

  3. How testing methods with nullable parameters usually get done in Kotlin ?

  4. How Mockito usually maps the whenever / when statements to the original methods ?


Feel free guys to answer any question of the 4. You don’t have answer them all.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật