I have numerous test cases that do something like this:
But because this is the first access of bar I get the following warning:
Method invocation 'getBar()' may produce 'NullPointerException'
I'm perfectly happy with test code throwing an NPE, after all, if it does, the test will fail. And that is what I want.
I don't want to apply the suggested fix of sticking Objects.requireNonNull() around it all as it just muddies the code. It is just a test after all. I also don't want to disable the warning across the project because it is useful in the main codebase. Is there a way to disable it just for tests? Much like the way you don't get warned about using property injection with @Autowired in a JUnit test. But you do get warned outside of tests.