During the execution of the rule, the obtained full name is "Test" rather than "org.junit.test"

The purpose of the rule is to prohibit the use of JUnit4. However, during the execution of the rule, the obtained full name is “Test” rather than “org.junit.test”, which leads to the failure of the rule’s operation.

However, when executing the rule in a normal environment, the rule runs correctly, which means that the error only occurs during testing.

rule code :slight_smile:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.tree.AnnotationTree;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.TypeTree;

import java.util.List;


@Rule(key = "JunitTestSupport")
public class JunitTestSupportRule extends BaseTreeVisitor implements JavaFileScanner {

    private static final Logger LOG = LoggerFactory.getLogger(JunitTestSupportRule.class);

    public static final String TEST_NAME = "Test";

    private JavaFileScannerContext context;

    @Override
    public void scanFile(JavaFileScannerContext context) {
        this.context = context;
        scan(context.getTree());
    }

    @Override
    public void visitMethod(MethodTree tree) {
        LOG.debug("JunitTestSupportRule visitMethod,tree ={}",tree.symbol().signature());
        List<AnnotationTree> annotations = tree.modifiers().annotations();
        for (AnnotationTree annotationTree : annotations) {
            TypeTree annotationType = annotationTree.annotationType();
            if (annotationType.is(Tree.Kind.IDENTIFIER)) {
                IdentifierTree identifier = (IdentifierTree) annotationType;
                if (isJunit4TestMethod(tree) && identifier.name().equals(TEST_NAME)) {
                    context.reportIssue(this, identifier, "新测试用例框架不支持junit4");
                }
            }
        }
        super.visitMethod(tree);
    }

    private static boolean isJunit4TestMethod(MethodTree tree) {
        return tree.symbol()
                .metadata()
                .isAnnotatedWith("org.junit.Test");
    }
}

test file :slight_smile:

import org.junit.Test;


public class JunitTestSupportRuleTest{
    @Test
    public void testSuccess() {
        Assert.assertEquals("0", "0");
    }
}

Hi,

Welcome to the community!

What version of SonarQube are you working against?

 
Thx,
Ann

sonarqube-10.4.1.88267

<sonar.plugin.api.version>9.14.0.375</sonar.plugin.api.version>
<sonarjava.version>7.28.0.33738</sonarjava.version>

Hey there, thanks for reaching out.

However, when executing the rule in a normal environment, the rule runs correctly, which means that the error only occurs during testing.

Can you elaborate more on what is a “normal environment”?

I think that in the non-working case, there is some missing semantic information about the annotation package. Maybe you did not compile your test sources before running your unit tests?

Without the compiled files in the classpath, the AST generated will miss semantic information and you will not have the fully qualified name of some classes/methods.

this is the same issue as this post: Local run on AssertionsInTestsCheckTest.java failed - SonarQube / Plugin Development - Sonar Community (sonarsource.com), All the annotation in junit test will ignore the imports.

normal environment means after the plugins are installed, both mvn and SonarLint can be executed normally.