Calling AboutTroubleInfoCollector.collectInfo() method throws NPE in tests because of '"com.intellij.ide.ui.LafManager.getCurrentUIThemeLookAndFeel()" is null'

Answered

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

1
6 comments

Hi Chris,

At first sight, it looks like a bug. What is the context it is called from?

1

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.

0

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?

0

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 and HeavyPlatformTestCase - and get this NPE. A minimal example is below:

package com.chriscarini.jetbrains.diagnostic.reporter;

import com.intellij.ide.troubleshooting.CompositeGeneralTroubleInfoCollector;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.IdeFrame;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;


public class GitHubErrorReportSubmitterTest extends BasePlatformTestCase {
    public void testAboutTroubleInfoCollectorDOTcollectInfo() {
        // when
        final String result = new CompositeGeneralTroubleInfoCollector().collectInfo(
                getLastFocusedOrOpenedProject()
        );

        // then
        assert !result.isEmpty();
    }

    private static Project getLastFocusedOrOpenedProject() {
        final IdeFrame lastFocusedFrame = IdeFocusManager.getGlobalInstance().getLastFocusedFrame();
        final Project project = lastFocusedFrame == null ? null : lastFocusedFrame.getProject();
        if (project == null) {
            final ProjectManager projectManager = ProjectManager.getInstance();
            final Project[] openProjects = projectManager.getOpenProjects();
            return openProjects.length > 0 ? openProjects[0] : projectManager.getDefaultProject();
        }
        return project;
    }
}

With this, the stacktrace I get is below (truncated to relevant part):

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)
	at com.chriscarini.jetbrains.diagnostic.reporter.GitHubErrorReportSubmitterTest.testAboutTroubleInfoCollectorDOTcollectInfo(GitHubErrorReportSubmitterTest.java:15)
...

 

Let me know if I am missing something or have set things up incorrectly. Happy to provide more information if you need, as well!

0

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

0

Please sign in to leave a comment.