How to test with PsiTypeClass.resolve()?

Answered

Hi,

The following line returns null in LightCodeInsightFixtureTestCase, FileEditorManagerTestCase, LightPlatformCodeInsightFixtureTestCase.

 

PsiClassType.getTypeByName("java.util.List", getProject(), GlobalSearchScope.allScope(getProject())).resolve()

 

Return empty map:

PsiClassType.getTypeByName("java.util.List", getProject(), lobalSearchScope.allScope(getProject())).resolveGenerics().getSubstitutor().getSubstitutionMap()

 

7 comments
Comment actions Permalink

Is mock JDK added to the test module? (ModuleRootManager#getSdk, IdeaTestUtil.getMockJdk18())

0
Comment actions Permalink

Thanks for the hint, but I have no clue how to put everything together  :(

What is the base class should be?

 

public class PsiClassTypeTest extends PlatformTestCase {

  public void setUp() throws Exception {
      super.setUp();
      ModuleRootManager.getInstance(getModule())
          .getModifiableModel().setSdk(IdeaTestUtil.getMockJdk18());
  }

  protected Sdk getTestProjectJdk() {
      return IdeaTestUtil.getMockJdk18(); //super.getTestProjectJdk();
  }

 

0
Comment actions Permalink

LightCodeInsightFixtureTestCase is a perfectly suitable base class, but please check if the returned mock jdk has meaningful content (i.e. its path contains the jars that should be in a mock jdk).

0
Comment actions Permalink

Thanks to JesusFreke from irc #idea-users,

Here it's a working test

https://github.com/JesusFreke/smali/blob/master/smalidea/src/test/java/org/jf/smalidea/ClassReferenceTest.java

 

In short there 2 points: extend  ResolveTestCase and override

protected Sdk getTestProjectJdk() {
    return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
}

 

0
Comment actions Permalink

I'm glad that you have it working. I'd still recommend to make use of mock JDK, since it's much smaller and faster to index, and so the test's first startup after index version change would be faster.

0
Comment actions Permalink

I have a similar case and I didn't get it working even when extending ResolveTestCase and overriding getTestProjectJdk().

The test blocks from first post return null in my case.

So I reverted to what I started with. What I am doing:

  • extend LightCodeInsightFixtureTestCase
  • set up fixture:
IdeaTestFixtureFactory factory = IdeaTestFixtureFactory.getFixtureFactory();
TestFixtureBuilder<IdeaProjectTestFixture> fixtureBuilder =
factory.createLightFixtureBuilder(JAVA_8);

final IdeaProjectTestFixture fixture = fixtureBuilder.getFixture();
testFixture = JavaTestFixtureFactory.getFixtureFactory().createCodeInsightFixture(fixture, new LightTempDirTestFixtureImpl(true));
testFixture.setTestDataPath("src/test/resources/mapping/");
testFixture.setUp();

However, methods are not resolved.

Peter wrote above that we should check JDK real path, for me it is mock JDK - I check it by

ModuleRootManager.getInstance(testFixture.getModule()).getSdk()

. How can I set it / change JDK when I am extending this LightCodeInsightFixtureTestCase?

I would really appreciate help here. The test file which does not work is https://github.com/hermanurikh/concbugs/blob/feature/add_idea_mapping/src/test/java/com/qbutton/concbugs/inspection/deadlock/mapping/PsiToAlgorythmFacadeTest.java , the test method - wait_success.

 

0
Comment actions Permalink

Just in case someone has the same problems, I managed to get resolve() working with LightCodeInsightFixtureTestCase by using following:

  • extend LightCodeInsightFixtureTestCase
  • override getProjectDescriptor():
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return new ProjectDescriptor(LanguageLevel.HIGHEST) {
@Override
public Sdk getSdk() {
return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
}
};
}
  • in setUp and tearDown you have to call super.setUp and super.tearDown! This part was not obvious for me - that those methods are implemented in parent and should be called, but they do not have beforeEach() on them and are not called automatically.
0

Please sign in to leave a comment.