Different behavior between tests and Dev instance


Hi everyone
I'm having a hard time figuring out why pattern isn't detected in test run (i use tdd).
I'm trying to get a pattern working inside a stream lambda, and this is my test file:

package org.apache.ofbiz;

import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.entity.util.EntityQuery;
import org.apache.ofbiz.service.LocalDispatcher;
import java.util.List;

public class EntityFieldCompletionInGetFieldInFilterStream {
    void testFunction(LocalDispatcher disp) throws GenericEntityException {
        Delegator delegator = disp.getDelegator();
        List<GenericValue> ents = EntityQuery.use(delegator).from("Party").queryList();
        ents.stream().filter(ent -> ent.get("<caret>") != null);

Now this doesn't work.
Here is my pattern

public static final String GENERIC_ENTITY_CLASS = 'org.apache.ofbiz.entity.GenericEntity'

public static final PsiElementPattern ENTITY_FIELD_COMPL = psiElement().inside(GENERIC_VALUE_FIELD)

public static final PsiElementPattern GENERIC_VALUE_FIELD = psiElement().andOr(
    makeGenericEntityJavaMethodPattern('get', 0),
static Capture<PsiLiteralExpression> makeGenericEntityJavaMethodPattern(String methodName, int index) {
    return makeMethodJavaPattern(methodName, GENERIC_ENTITY_CLASS, index)

static Capture<PsiLiteralExpression> makeMethodJavaPattern(String methodName, String className, int index) {
    return makeMethodPattern(PsiJavaPatterns::literalExpression(), methodName, className, index)

static Object makeMethodPattern(PsiJavaElementPattern<? extends PsiLiteral, ?> elementPattern,
        String methodName, String className, int index) {
return elementPattern.methodCallParameter(index, PsiJavaPatterns.psiMethod().withName(methodName).definedInClass(className))

I use the ENTITY_FIELD_COMPL pattern for registering completion contributor.

Now, with the test file, the pattern doesn't seem to work, but when i launch the dev instance with the exact same method, the pattern works.

The only difference between the dev instance and the test file are the class name, and the package name.
Does anyone has an idea ?



It's hard to tell the reason with this information, but I guess it is a test project setup issue. I suggest debugging the code to see what is missing.


Thanks for your answer Karol. Seems big but i'll try.
Do you maybe have some starting point ? 


I suggest the following approach:

  1. Run the dev instance in debug mode.
  2. Add a breakpoint in your completion provider.
  3. Invoke completion. The execution will stop at the breakpoint.
  4. See the stacktrace and try to find a good higher-level invocation point where you can add a breakpoint to debug a non-working case. Try to find a place executed just before completion patterns are executed (or earlier, it depends on the issue reason).
  5. After that, execute your tests and dev instance in debug mode and compare why it is not executed in the first case.

Hello again Karol.
I tried the approach you recomended.

It's even more confusing. I went up to CompletionContributor.java:154 (i'm still using version 223 because some other issue)

- In the case of the test run, the condition of pattern is false, so the completion doesn't start.
- In the case of the dev instance, the pattern condition is true, and the completion fires up.

I compared the PsiStructure using the Psi view built in tool, and the psi is EXACTLY the same in test file and dev instance
In one case, pattern is ok, and in the other it isn't.

I thought maybe of an import or compilation issue in test setup, but in other tests i use the same imports as the ones in the faling test file, so that would be surprising.
But that's my only guess for now.

Is there a way in fixture to get errors detected in file that would prevent pattern matching ? 



Is there a way in fixture to get errors detected in file that would prevent pattern matching ? 

I don’t think so. Missing imports or something breaking resolving is a good trail. I suggest debugging patterns accept() methods and methods around, e.g.:

  • accept() in PsiJavaElementPattern.methodCallParameter(ElementPattern<? extends PsiMethod>)
  • processValues() in PsiMethodPattern#definedInClass(ElementPattern<? extends PsiClass>)
  • and other similar places

If the problem is that class or method cannot be resolved, then you should review your test context setup.


Please sign in to leave a comment.