Calling AboutTroubleInfoCollector.collectInfo() method throws NPE in tests because of '"com.intellij.ide.ui.LafManager.getCurrentUIThemeLookAndFeel()" is null'
Hello,
I have some code that calls into AboutTroubleInfoCollector.collectInfo()
that I am trying to test in a unit test, but doing so throws the below NPE:
java.lang.NullPointerException: Cannot invoke "com.intellij.ide.ui.laf.UIThemeLookAndFeelInfo.getName()" because the return value of "com.intellij.ide.ui.LafManager.getCurrentUIThemeLookAndFeel()" is null
at com.intellij.ide.troubleshooting.AboutTroubleInfoCollector.collectInfo(AboutTroubleInfoCollector.java:46)
at com.intellij.ide.troubleshooting.CompositeGeneralTroubleInfoCollector.collectInfo(CompositeGeneralTroubleInfoCollector.java:19)
at com.intellij.ide.troubleshooting.CompositeGeneralTroubleInfoCollector.collectInfo(CompositeGeneralTroubleInfoCollector.java:12)
I've already tried LafManager.getInstance().setCurrentLookAndFeel()
but it appears that during test, LafManager.getInstance()
returns a HeadlessLafManagerImpl
, which has an empty impl of the setCurrentLookAndFeel()
method, so that does not appear to work.
What is the best/suggested way to mock LafManager.getInstance().getCurrentUIThemeLookAndFeel().getName()
in a test? Please advise!
Best,
Chris
Please sign in to leave a comment.
Hi Chris,
At first sight, it looks like a bug. What is the context it is called from?
Hi Karol! I hope you are doing well! Happy new years!
I am using `AboutTroubleInfoCollector.collectInfo()` as part of my plugin(s) `ErrorReportSubmitter` extension point. Specifically, I have code extracted into a library I plan on using across my 6+ published plugins, and I want to write some simple unit tests in the library.'
Let me know if you have any suggestions, even if they are temporary while this ‘bug’ gets fixed.
Hi Chris,
Happy new year to you too 🙂
By the calling context, I mean the test implementation. Does your test base class inherit classes from the platform testing framework? Is the context properly set up?
Hi Karol,
(facepalm) haha, sorry for my misunderstanding! Note to self; don't program/respond too late/early :)
Yes, I've tried a few ways inheriting from platform testing framework - both
BasePlatformTestCase
andHeavyPlatformTestCase
- and get this NPE. A minimal example is below:With this, the stacktrace I get is below (truncated to relevant part):
Let me know if I am missing something or have set things up incorrectly. Happy to provide more information if you need, as well!
Hi Chris,
Sorry for the delay. It looks set up correctly and, IMHO, there is a bug in AboutTroubleInfoCollector - there should be a null check for getting the LaF, so it can work in headless IDE, even for testing purposes. I suggest reporting it on YouTrack: https://youtrack.jetbrains.com/issues/IDEA
Thank you Karol, I've opened https://youtrack.jetbrains.com/issue/IDEA-345108/Calling-AboutTroubleInfoCollector.collectInfo-method-throws-NPE-in-tests-because-of and I hope to have a PR for a fix up shortly.
Update: Opened https://github.com/JetBrains/intellij-community/pull/2660