0

I have an internal extension method:

@VisibleForTesting
internal fun URL.normalizeUrl() {some code not related to this question}

I want to test this method in a JUnit test

val normalizedUrl = someUrl.normalizeUrl()

But the method is invisible for the test class, I get Unresolved reference: normalizeUrl in IntelliJ.

Is this possible to test this method without modifying the access level?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
hc0re
  • 1,806
  • 2
  • 26
  • 61
  • Is your code testing located in a different module? If yes, could you move that code into the module where `normalizeUrl` is defined? – Lino Jul 06 '20 at 13:22
  • @Lino-Votedon'tsayThanks nope, the code is in the same module as tests. Code is under `src/main/java/some/package` and tests are under `/src/test/java/some/package` – hc0re Jul 06 '20 at 13:24
  • 6
    One philosophy of testing is that you should not test the "private" implementation, you should only test what's publicly observable. That way the tests are less likely to break when the implementation changes because it's optimized, or refactored to add support for a new feature. – Joni Jul 06 '20 at 13:25
  • See also https://stackoverflow.com/questions/105007/should-i-test-private-methods-or-only-public-ones – Joni Jul 06 '20 at 13:26

0 Answers0