How to setup JavaCodeInsightFixtureTestCase so PsiReferenceExpression reference resolves to non-null
Answered
I am trying to setup JavaCodeInsightFixtureTestCase based test to allow me to test code which finds Map.put expressions in a Java class file.
I have the code working in the plugin but the tests fail because trying to resolve reference from PsiReferenceExpression of optionsMap.put in the following code, to PsiMethodReference which in test always returns null but in non-test use returns the expected Map::put method reference.
public class SampleTest extends ComboSpecTestCase {
final private static Map<String, DataHolder> optionsMap = new HashMap<>();
static {
optionsMap.put("self", new MutableDataSet().set(SELF, true));
optionsMap.put("margin", new MutableDataSet().set(CUSTOM_OPTION, SampleTest::marginOption));
}
}
At first I thought it was caused by not using a real Java SDK so added bundled JDK 11 to the test project configuration, with language level set to LanguageLevel.JDK_1_8. This part seems to work but does not solve the problem of getting null from reference.resolve().
import com.intellij.testFramework.LightProjectDescriptor;
public class JavaCodeInsightFixtureSpecTestCase extends JavaCodeInsightFixtureTestCase implements CodeInsightFixtureSpecTestCase {
@Before
public void before() throws Throwable {
setUp();
// setup
Application application = ApplicationManager.getApplication();
SpecTestCaseJavaProjectDescriptor projectDescriptor = getProjectDescriptor();
application.invokeAndWait(projectDescriptor::setupAndAddSdk);
LanguageLevelProjectExtension.getInstance(getProject())
.setLanguageLevel(projectDescriptor.getLanguageLevel());
Module module = getModule();
ModuleRootManagerEx rootManagerEx = ModuleRootManagerEx.getInstanceEx(module);
final ModifiableRootModel[] modifiableModel = new ModifiableRootModel[1];
final ContentEntry[][] entries = new ContentEntry[1][1];
application.runReadAction(() -> {
modifiableModel[0] = rootManagerEx.getModifiableModel();
entries[0] = modifiableModel[0].getContentEntries();
});
projectDescriptor.configureModule(module, modifiableModel[0], entries[0].length > 0 ? entries[0][0] : null);
}
}
// NOTE: library paths shortened from full system paths.
class SpecTestCaseJavaProjectDescriptor implements LightProjectDescriptor {
final LanguageLevel myLanguageLevel = LanguageLevel.JDK_1_8;
final String[] myModuleLibraries = {
"lib/annotations-18.0.0.jar",
"lib/flexmark-parent.jar",
"lib/flexmark-util.jar",
"lib/flexmark-test-util.jar",
"lib/junit-4.12.jar",
"lib/hamcrest-core-1.3.jar",
};
Sdk mySdk = null;
@Override
public void setupAndAddSdk() {
SdkType[] types = SdkType.getAllTypes();
SdkType javaSdk = SdkType.findInstance(JavaSdkImpl.class);
String sdkPath = "/Applications/IntelliJ-IDEA-2019.3-CE-EAP.app/Contents/jbr/Contents/Home";
mySdk = SdkConfigurationUtil.createAndAddSDK(sdkPath, javaSdk);
assert mySdk != null : "Could not load SDK: " + sdkPath;
}
@Override
public Sdk getSdk() {
return mySdk == null ? super.getSdk() : mySdk;
}
@Override
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
model.getModuleExtension(LanguageLevelModuleExtension.class).setLanguageLevel(myLanguageLevel);
PsiTestUtil.addProjectLibrary(module, "test-lib", Arrays.asList(myModuleLibraries));
}
}
Please sign in to leave a comment.
What about com.intellij.testFramework.builders.JavaModuleFixtureBuilder#addJdk?
Thanks Yann. I will try it.
Thank you Yann!
It works like a charm and simplified my test setup to a clean: