0

I am new to unit testing, and facing issue with a extension function, which is kind of:

fun List<Water>.getFileteredList() = return listOf(filteredWater)

Now if I want to test this function, how can I provide the list to the function while testing.

Water is just and example in this scenario.

Ankush
  • 175
  • 1
  • 12
  • It looks like your extension method is inside another class and referencing another field. Why? This makes it pretty hard to test and doesn't really make sense? – Kilian Sep 10 '21 at 05:13
  • Usually you would be able to test extension methods by constructing the object for which the extension method was made. In your example that would mean creating a list of Water and just calling the extension method in your test. IE: listOf(Water()).getFilteredList() – Kilian Sep 10 '21 at 05:14
  • @Kilian , Actually I have centralized few similar type of extension functions inside a singleton class. Just to make the calling class more readable. – Ankush Sep 10 '21 at 06:20
  • Also, I am having confusion, is it right to convert a sample json.txt file to json object and get the list from that, in a test class? – Ankush Sep 10 '21 at 06:21
  • 1
    Try to make your objects more isolated, it looks like your water has a weird dependency on that Singleton making it hard to write tests for. – Kilian Sep 10 '21 at 18:18
  • I did it. But yes you are right, it has become much more complex. Unit test should run fast. I need to make the class less cluttered to make testing easier now. Thanks @Kilian – Ankush Sep 11 '21 at 15:45

0 Answers0