Can't access final fields in other component


(Anne Stellingwerf) #1

Observe https://github.com/astellingwerf/package-private-final-gradle-issue. It contains 3 files:

  • build.gradle (obviously the build logic)
  • src/main/java/nl/astellin/Main.java (the production code)
  • src/bvt/java/nl/astellin/MainTest.java (a build validation/unit test (sorta))

The build.gradle creates two components for the production code (main) and the unit tests (bvt). The package private static field in Main is not necessarily part of the API to other components and other projects, but I’d like to assert it’s state in the bvt. (The code isn’t as in real life, it’s the bare minimum I could come up with the prove my issue.)

Now, it looks like, the second I make the field in Main final, it seems to get dropped from the API jar, and I therefore can’t access it in my unit tests.

My questions:

  1. Is this intentional?
  2. What’s the reasoning why a final field couldn’t be part of the API?
  3. What can I do to allow access for the unit tests, without exposing it to other components? Am I doing it right, and can/should test code be a component?