This rule raises an issue when a language’s built-in assertion statement is used within a test method, identified by test framework annotations or markers that designate methods as test cases.

Why is this an issue?

Native assertion statements in many languages are inappropriate for test code because they can be disabled or controlled by runtime configuration. Unless explicitly enabled through compiler flags, runtime options, or environment settings, these assertion statements may do nothing—your test will pass even when the condition fails.

This creates a false sense of security. Tests that appear to pass may not actually be verifying anything.

Additionally, when native assertions do execute, they provide minimal diagnostic information. A failed assertion typically throws a generic error with little context about what went wrong. In contrast, testing framework assertion methods provide:

Testing frameworks are specifically designed for writing tests and handle assertions properly in all execution modes.

What is the potential impact?

Using assertion mechanisms that can be disabled through runtime configuration in tests can lead to:

How to fix it in JUnit 4

Replace the Java assert statement with JUnit 4’s static assertion methods from the org.junit.Assert class. Use methods like assertEquals(), assertTrue(), assertFalse(), assertNull(), etc., which provide clear error messages and always execute.

Code examples

Noncompliant code example

import org.junit.Test;

public class FooTest {
    @Test
    public void testSomething() {
        assert Foo.bar == 1; // Noncompliant
    }
}

Compliant solution

import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class FooTest {
    @Test
    public void testSomething() {
        assertEquals(1, Foo.bar);
    }
}

How to fix it in JUnit 5

Replace the Java assert statement with JUnit 5’s static assertion methods from the org.junit.jupiter.api.Assertions class. JUnit 5 provides improved assertion methods with better messages and optional custom failure messages.

Code examples

Noncompliant code example

import org.junit.jupiter.api.Test;

public class FooTest {
    @Test
    void testSomething() {
        assert Foo.bar == 1; // Noncompliant
    }
}

Compliant solution

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FooTest {
    @Test
    void testSomething() {
        assertEquals(1, Foo.bar, "Foo.bar should be 1");
    }
}

How to fix it in TestNG

Replace the Java assert statement with TestNG’s static assertion methods from the org.testng.Assert class. TestNG assertions provide detailed failure messages and integrate with TestNG’s reporting features.

Code examples

Noncompliant code example

import org.testng.annotations.Test;

public class FooTest {
    @Test
    public void testSomething() {
        assert Foo.bar == 1; // Noncompliant
    }
}

Compliant solution

import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;

public class FooTest {
    @Test
    public void testSomething() {
        assertEquals(Foo.bar, 1);
    }
}

Resources

Documentation