JetBrains/intellij-community · error · NoJUnitException

No junit.jar

Error message

No junit.jar

What it means

JUnitUtil.getTestCaseClass(Module) throws NoJUnitException (bundle message 'No junit.jar') when the passed Module is null. The JUnit 3 'TestCase' support class cannot be looked up without a module to derive the runtime scope from. NoJUnitException is the standard signal that JUnit is missing from the execution scope.

Source

Thrown at java/execution/impl/src/com/intellij/execution/junit/JUnitUtil.java:500

  private static boolean isExplicitlyTestAnnotated(PsiMethod method) {
    return TestUtils.isExplicitlyJUnit4TestAnnotated(method) ||
           JUnitRecognizer.willBeAnnotatedAfterCompilation(method) ||
           isJupiterTestAnnotated(method, true);
  }

  public static boolean isJUnit4TestAnnotated(PsiMethod method) {
    return AnnotationUtil.isAnnotated(method, TEST_ANNOTATION, CHECK_HIERARCHY) || JUnitRecognizer.willBeAnnotatedAfterCompilation(method);
  }

  private static @Nullable PsiClass getTestCaseClassOrNull(final PsiClass psiClass) {
    Module module = ModuleUtilCore.findModuleForPsiElement(psiClass);
    if (module == null) return null;
    GlobalSearchScope scope = GlobalSearchScope.moduleRuntimeScope(module, true);
    return getTestCaseClassOrNull(scope, module.getProject());
  }

  public static @NotNull PsiClass getTestCaseClass(final Module module) throws NoJUnitException {
    if (module == null) throw new NoJUnitException();
    final GlobalSearchScope scope = GlobalSearchScope.moduleRuntimeScope(module, true);
    return getTestCaseClass(scope, module.getProject());
  }

  public static @NotNull PsiClass getTestCaseClass(final SourceScope scope) throws NoJUnitException {
    if (scope == null) throw new NoJUnitException();
    return getTestCaseClass(scope.getLibrariesScope(), scope.getProject());
  }

  public static void checkTestCase(SourceScope scope, Project project) throws NoJUnitException {
    if (scope == null) throw new NoJUnitException();
    PsiPackage aPackage = JavaPsiFacade.getInstance(project).findPackage("junit.framework");
    if (aPackage == null || aPackage.getDirectories(scope.getLibrariesScope()).length == 0) {
      throw new NoJUnitException();
    }
  }

  private static @NotNull PsiClass getTestCaseClass(final @NotNull GlobalSearchScope scope, final @NotNull Project project)

View on GitHub (pinned to be881553f2)

Solutions

  1. Ensure the file/class belongs to a real module content root (move it out of scratch/library source roots)
  2. Pass an explicitly resolved Module: ModuleUtilCore.findModuleForPsiElement(psi) and check for null before calling
  3. Wait for project import/sync to finish before running JUnit-related actions
  4. If writing plugin code, guard: if (module == null) handle the no-module case instead of calling this API

Example fix

// before
PsiClass testCase = JUnitUtil.getTestCaseClass(module); // module may be null
// after
PsiClass testCase = module != null ? JUnitUtil.getTestCaseClass(module) : null;
Defensive patterns

Strategy: type-guard

Validate before calling

Module module = ModuleUtilCore.findModuleForPsiElement(psiClass);
if (module == null) return null; // class outside any module: JUnit lookup impossible

Type guard

boolean canLookupTestCase(Module m) {
  return m != null && !m.isDisposed() && GlobalSearchScope.moduleRuntimeScope(m, true) != null;
}

Try / catch

try {
  PsiClass tc = JUnitUtil.getTestCaseClass(module);
} catch (NoJUnitException e) {
  // junit not on module runtime scope: prompt to add the dependency
  suggestAddJUnit(module);
}

Prevention

When it happens

Trigger: Calling getTestCaseClass with a null Module — e.g. a PSI class not belonging to any module (default project content, scratch file outside modules), or ModuleUtilCore.findModuleForPsiElement returning null upstream.

Common situations: JUnit 3 test infrastructure invoked on a file outside module content roots (scratch files, files in libraries sources), or during project import when modules are not yet configured.

Related errors


AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14). Data as JSON: /api/errors/5ca52c9d50934252. Report an issue: GitHub.