GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Missing @Nested annotation on JUnit 5 inner test class — CodeQL query help documentation

Missing @Nested annotation on JUnit 5 inner test class¶

ID: java/junit5-missing-nested-annotation
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
Tags:
   - quality
   - reliability
   - correctness
   - testability
   - frameworks/junit
Query suites:
   - java-code-quality.qls

Click to see the query in the CodeQL repository

Overview¶

JUnit tests are grouped in a class, and starting from JUnit 5, users can group the test classes in a larger class so they can share the local environment of the enclosing class. While this helps organize the unit tests and foster code reuse, if an inner test class is not annotated with @Nested, the unit tests in it will fail to execute during builds.

Recommendation¶

If you want the tests defined in an inner class to be recognized by the build plugin and be executed, annotate the class with @Nested, imported from org.junit.jupiter.api.

Example¶

import org.junit.jupiter.api.Nested;
import static org.junit.Assert.assertEquals;

public class IntegerOperationTest {
  private int i;  // Shared variable among the inner classes.

  @BeforeEach
  public void initTest() { i = 0; }

  @Nested
  public class AdditionTest {  // COMPLIANT: Inner test class annotated with `@Nested`.
    @Test
    public void addTest1() {
      assertEquals(1, i + 1);
    }
  }

  public class SubtractionTest {  // NON_COMPLIANT: Inner test class missing `@Nested`.
    @Test
    public void addTest1() {
      assertEquals(-1, i - 1);
    }
  }
}

Implementation Notes¶

This rule is focused on missing @Nested annotations on non-static nested (inner) test classes. Static nested test classes should not be annotated with @Nested. As a result, the absence of a @Nested annotation on such classes is compliant. Identifying incorrect application of a @Nested annotation to static nested classes is out of scope for this rule.

References¶

  • JUnit 5 API Documentation: Annotation Interface Nested.

  • JUnit 5 User Guide: Nested Tests.

  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.