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)
@ActiveProfiles(profiles = {"test"})
public class FrontendLogIntegrationTests {
    private TestApiClient client;

    Logger logger;

    private FrontendLogController controller;

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

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

And a controller class

public class FrontendLogController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    public void logMessages(@RequestBody List<FrontendLog> messages) {
        for(var message : messages) {
  "{}", 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.


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