Intellij IDEA plugin unit testing (test project dependencies)

I have Intellij IDEA plugin that simply finds org.jetbrains.annotations.NotNull annotation and marks it as error:

class JetBrainsNullabilityAnnotations : BaseJavaLocalInspectionTool() {

    ...

    override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
        return object : JavaElementVisitor() {
            override fun visitAnnotation(annotation: PsiAnnotation) {
                super.visitAnnotation(annotation)
                if (annotation.qualifiedName == "org.jetbrains.annotations.NotNull") {
                    // register problem
                }
            }

        }
    }

}

But when I'm running inspection under junit test (InspectionTestCase) annotation.qualifiedName returns just NotNull (in real IDE this expression returns string org.jetbrains.annotations.NotNull). Test file:

import org.jetbrains.annotations.NotNull;

public interface Foo {
    void bar(@NotNull Object object);
}

I tried to add annotation source code manually to test project src dir (testData/myTestDir/src/NotNull.java):

package org.jetbrains.annotations;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD, java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.LOCAL_VARIABLE})
public @interface NotNull
{
  String value() default "";
}

And I tried to add annotations as module dependency:

override fun setUpModule() {
    super.setUpModule()
    PsiTestUtil.addLibrary(module, "./libs/annotations.jar")
}

But still had no luck to get fully qualified name of annotation in unit test environment. How to properly provide dependencies?

1

I'd rather do myFixture.addClass("

package org.jetbrains.annotations;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Documented
@Retention(RetentionPolicy.CLASS)
@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.FIELD, java.lang.annotation.ElementType.PARAMETER, java.lang.annotation.ElementType.LOCAL_VARIABLE})
public @interface NotNull
{
  String value() default "";
}

")

 

if you use fixtures. 

Hope it helps,

Anna

0
Avatar
Permanently deleted user

No, I'm using 

 com.intellij.testFramework.InspectionTestCase

because it handles inspection running/asserting itself. Is there any way to add class using string literal in this case? Or how can I debug this problem?

0

请先登录再写评论。