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?


at com.intellij.codeInsight.lookup.impl.LookupManagerImpl.getActiveLookup( // myActiveLookup == null
at com.intellij.codeInsight.lookup.LookupManager.getActiveLookup(
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.getLookup(
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.getLookupElements( // myEmptyLookup = false, lookup <- null => return null
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl$21.compute(
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl$21.compute(
at com.intellij.util.ui.UIUtil$
at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(
at com.intellij.util.ui.UIUtil.invokeAndWaitIfNeeded(
at com.intellij.testFramework.fixtures.impl.CodeInsightTestFixtureImpl.complete( // returns null




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


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

Test class:

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

public void testStaticImportCompletion() {
myFixture.configureByFiles("", "");
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");

namespace some

import zzz.ZZLong

record RRR {
field: Z<caret>

namespace zzz

long ZZLong





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

Please change order in arguments in

myFixture.configureByFiles("", "");

so that file containing <caret> is last.

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


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 When I added a <caret> to, I got nulls again.

As a test I reverted configureByFiles call and added this:

myFixture.configureByFiles("", "");
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.


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).


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

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



Konstantin Sobolev hey mate, I also have similar situation. Which method did u use instead of complete()?


Please sign in to leave a comment.