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
?