I use PowerMock to model static methods on JOptionPane , but the JRE does not seem to be very consistent with it because I get java.lang.VerifyError on initialization because it checks the integrity of its packages and classes.
I have some workarounds, but I'm not very happy with any of them:
Write an object wrapper for JOptionPane and provide an interface for the methods I need ( showInputDialog , etc.), so I can enter a layout or stub for testing. This will simply move the problem elsewhere, since I still have to cover my wrapper methods, but at least they will be isolated from the logic.
Use an instance of JOptionPane instead of a class reference to invoke methods on it (I think I will not have problems mocking the instance since the class is not final). The downside is that I will get a lot of warnings that โcalls the static method on the instance variableโ, but this is the price you will pay.
Do not fake JOptionPane at all and use Robot to fire input events to handle it. This can be very cumbersome and not very reliable ... In addition, I use internal dialogs, and this requires additional work to configure JDesktopPane , JInternalFrame , etc.
Any other ideas or suggestions?
Thanks!
update: by te way, I tried to mock the JOptionPane instance, and it seems that the method manager is ignoring the instance that directly selects the previously existing static method (in the end, it makes sense), so the second option is discarded.
source share