0

I have a simple class like this

public class Version {
    public static final Version SOME_VER = new Version("1.2.3");
    ...
}

Now, up to recently I was able to change the SOME_VER field via reflection in a junit test, but due to the changes regarding reflection in Java 12 this is not possible anymore.
I know I could use powermock or instrumentation to change the class, but for several reasons do not want that.

Is there a way a different way to change the SOME_VER in a test? Or how would I change the Version class, so I can have a different version SOME_VER set on class creation?

sveri
  • 1,372
  • 1
  • 13
  • 28
  • your SOME_VER doesn't even have a type. I'm surprised this even compiles – Stultuske Oct 25 '22 at 08:05
  • @Stultuske true, I added it, typed it away from my memory, but I think the point is clear despite that? – sveri Oct 25 '22 at 08:12
  • 1
    I have an idea but it's a longshot. Instead of using `private static final`, take the value from properties and override in tests? – Fenio Oct 25 '22 at 08:13
  • 1
    In such cases I prefer to use profiles. you can have different profiles for test/production and inject the version from `application.properties` into SOME_VER – Behnam Safari Oct 25 '22 at 08:15
  • 1
    @sveri the type can have an impact. it can be either mutable or immutable. – Stultuske Oct 25 '22 at 08:34
  • Does this answer your question? [Mock private static final field using mockito or Jmockit](https://stackoverflow.com/questions/30703149/mock-private-static-final-field-using-mockito-or-jmockit) – pringi Oct 31 '22 at 15:27

0 Answers0