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.

6
  • 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? Commented Sep 10, 2021 at 5: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() Commented Sep 10, 2021 at 5:14
  • @Kilian , Actually I have centralized few similar type of extension functions inside a singleton class. Just to make the calling class more readable. Commented Sep 10, 2021 at 6: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? Commented Sep 10, 2021 at 6: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. Commented Sep 10, 2021 at 18:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.