Skip to content

False postive UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR in TestNG test with @BeforeClass #3152

@mbert

Description

@mbert

SpotBugs version: 4.8.6.4 (but the bug seems to occur in all 4.8 versions)
Java: 21
Consider the attached mini project: spotbugs.tar.gz

The code passes SB versions < 4.8, but now we are getting this false positive:

package org.example;

import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.concurrent.atomic.AtomicInteger;

public class SpotBugsFalsePositiveTest {

    private AtomicInteger initializedMember;

    @BeforeClass
    public void setup() {
        initializedMember = new AtomicInteger();
    }

    @Test
    public void testSyncMasterData() {
        Assert.assertEquals(initializedMember.get(), 0);
    }
}

Expected Behaviour: the code should pass SB, because @BeforeClass is supposed to be treated like a constructor.

Actual behaviour:

[ERROR] Low: SpotBugsFalsePositiveTest.initializedMember not initialized in constructor and dereferenced in org.example.SpotBugsFalsePositiveTest.testSyncMasterData() [org.example.SpotBugsFalsePositiveTest] At SpotBugsFalsePositiveTest.java:[line 20] UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR

See also #453 from 2017 where a similar false positive was then fixed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions