Completion Unit Test: complete returns null

Can't get completion unit test to work. I'm following the guide, but `getLookupElementStrings()` (or result of `complete(...)`) is always null. Completion actually works when I try it in the IDE.

I did a bit of debugging and it boils down to LookupManagerImpl.myActiveLookup being null when getting results. What could be the cause?

Stacktrace:

at com.intellij.codeInsight.lookup.impl.LookupManagerImpl.getActiveLookup(LookupManagerImpl.java:225) // myActiveLookup == null
at com.intellij.codeInsight.lookup.LookupManager.getActiveLookup(LookupManager.java:42)
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.getLookup(CodeInsightTestFixtureImpl.java:1903)
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.getLookupElements(CodeInsightTestFixtureImpl.java:1274) // myEmptyLookup = false, lookup <- null => return null
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl$21.compute(CodeInsightTestFixtureImpl.java:1214)
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl$21.compute(CodeInsightTestFixtureImpl.java:1195)
at com.intellij.util.ui.UIUtil$13.run(UIUtil.java:2455)
at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(UIUtil.java:2426)
at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(UIUtil.java:2452)
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.complete(CodeInsightTestFixtureImpl.java:1195) // returns null

thanks!

 

6 comments

Please show the whole test method's code and verify you have "<caret>" marker in the test data file where completion is invoked on.

0

Sure, please find them below. I can confirm that getVariants on my reference gets invoked correctly.

Test class:

public class CompletionTest extends LightCodeInsightFixtureTestCase {
@Override
protected String getTestDataPath() {
return "src/test/resources/testData/completion";
}

public void testStaticImportCompletion() {
myFixture.configureByFiles("CompletionWithStaticImport.es", "zzz.es");
LookupElement[] complete = myFixture.complete(CompletionType.BASIC, 1); // this is null
List<String> strings = myFixture.getLookupElementStrings(); // this is null too
// assertNotNull(strings);
// assertEquals(1, strings.size());
// assertContainsElements(strings, "ZZLong");
}
}

CompletionWithStaticImport.es:

namespace some

import zzz.ZZLong

record RRR {
field: Z<caret>
}

zzz.es:

namespace zzz

long ZZLong

thanks

 

0

WRONG, PLEASE IGNORE

It seems the javadoc for com.intellij.testFramework.fixtures.CodeInsightTestFixture#configureByFiles is misleading, good catch.

Please change order in arguments in

myFixture.configureByFiles("CompletionWithStaticImport.es", "zzz.es");

so that file containing <caret> is last.

AFAIU copyFileToProject() for zzz.es should be enough and slightly faster.

0

Hm, I'm not sure you're correct.. I did as you said and got non-null results, but looks like it tried to invoke completion at the very beginning of zzz.es. When I added a <caret> to zzz.es, I got nulls again.

As a test I reverted configureByFiles call and added this:

myFixture.configureByFiles("CompletionWithStaticImport.es", "zzz.es");
PsiElement element = myFixture.getFile().findElementAt(myFixture.getCaretOffset()); // <--
LookupElement[] complete = myFixture.complete(CompletionType.BASIC, 1);

Inspecting `element` in the debugger shows that the framework got it right:

(field_decl ('field' ':' ' ' (type_ref ...)) ' ' '}'

element is that last PsiWhiteSpace before '}'.

 

Besides, I have another test that uses configureByFiles(..) with 2 arguments, and having <caret> in the first of them works just fine.

0

You're right, I missed the loop iterates backwards and got additionally confused by looking at a sample where both files have <caret>.

Did you check there are multiple completion variants at this position? <null> is expected result if "if the only item was auto-completed" (javadoc).

0

There's only one variant indeed.. how do I check for it then?

Edit: nevermind, found it. Just another method in CodeInsightTestFixture.

Thanks!

0

Please sign in to leave a comment.