java:S5979 doesn't account for @SpringBootTest

This is an issue with java:S5979.
version: Developer EditionVersion 8.7 (build 41497)

I have a @SpringBootTest which uses a mock.

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@EmbeddedKafka
@ActiveProfiles(profiles = {"test"})
public class FrontendLogIntegrationTests {
    @Autowired
    private TestApiClient client;

    @Mock
    Logger logger;

    @Autowired
    private FrontendLogController controller;

    @BeforeEach
    void setup() throws NoSuchFieldException, IllegalAccessException {
        ReflectionTestUtils.setField(controller, "logger", logger);
    }

    @Test
    void shouldLogMessages() {
        var log = FrontendLog.builder().level("info").data(List.of("hello")).build();
        client.createFrontendLog(List.of(log));
        then(logger).should().info("{}", log);
    }
}

And a controller class

@RestController
@RequestMapping("/api/v1/frontend-log")
public class FrontendLogController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @PutMapping
    public void logMessages(@RequestBody List<FrontendLog> messages) {
        for(var message : messages) {
            logger.info("{}", message);
        }
    }
}

The test passes and works fine but java:S5979 says the mock is not initialized. From what I have seen in test examples @SpringBootTest initializes the mocks. Can this rule also check for @SpringBootTest?

Hello @moaxcp

The problem you described is indeed a false positive.
The good news is that this problem is already fixed in version 6.15 of the Java analyzer, see SONARJAVA-3734. The fix is available in SonarQube versions >= 8.9.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.