Java.lang.VerifyError with mocking static Swing methods

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.

+4
source share
1 answer
  • Write a wrapper for JOptionPane - this is by far the most reliable option, and also allows you to write convenient manual methods for you. I would choose this one. If, like I and most other developers, you already have some kind of helper class GUI in your project, they can get there.

  • Using an instance is not a bad decision, but it is definitely not as easy to manage as calling a single static method. I would not say that the added complexity was worth it.

  • Use Robot to mock inputs - yes, that sounds very fragile to me. You become dependent on the internal structure and implementation details of JOptionPane at this point, which is not a good place. JOptionPane 's behavior and order of buttons may also vary depending on different views (e.g. OK, Cancel or Cancel, OK). Finally, this will not work in a headless environment (although if you already use JOptionPane in your tests and plan to always test on a desktop computer, this is not a problem).

+2
source

Source: https://habr.com/ru/post/1345688/


All Articles