From f32b29e166ccb3f5fcc00d256314038fe4d63fbc Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 28 Jan 2025 21:12:28 -0800 Subject: [PATCH] Don't use mocks for `SystemStatusListenerTests` See gh-44012 --- .../logback/SystemStatusListenerTests.java | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SystemStatusListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SystemStatusListenerTests.java index 0d98cc61fc9..196d7236056 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SystemStatusListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SystemStatusListenerTests.java @@ -16,10 +16,10 @@ package org.springframework.boot.logging.logback; -import java.util.List; import java.util.function.Supplier; import ch.qos.logback.classic.LoggerContext; +import ch.qos.logback.core.BasicStatusManager; import ch.qos.logback.core.status.ErrorStatus; import ch.qos.logback.core.status.InfoStatus; import ch.qos.logback.core.status.Status; @@ -28,17 +28,12 @@ import ch.qos.logback.core.status.StatusManager; import ch.qos.logback.core.status.WarnStatus; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.test.util.ReflectionTestUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.mock; /** * Tests for {@link SystemStatusListener}. @@ -51,13 +46,12 @@ class SystemStatusListenerTests { private static final String TEST_MESSAGE = "testtesttest"; - private final StatusManager statusManager = mock(StatusManager.class); + private final StatusManager statusManager = new BasicStatusManager(); - private final LoggerContext loggerContext = mock(LoggerContext.class); + private final LoggerContext loggerContext = new LoggerContext(); SystemStatusListenerTests() { - given(this.loggerContext.getStatusManager()).willReturn(this.statusManager); - given(this.statusManager.add(any(StatusListener.class))).willReturn(true); + this.loggerContext.setStatusManager(this.statusManager); } @Test @@ -104,8 +98,9 @@ class SystemStatusListenerTests { @Test void shouldRetrospectivePrintStatusOnStartAndDebugIsDisabled(CapturedOutput output) { - given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(new ErrorStatus(TEST_MESSAGE, null), - new WarnStatus(TEST_MESSAGE, null), new InfoStatus(TEST_MESSAGE, null))); + this.statusManager.add(new ErrorStatus(TEST_MESSAGE, null)); + this.statusManager.add(new WarnStatus(TEST_MESSAGE, null)); + this.statusManager.add(new InfoStatus(TEST_MESSAGE, null)); addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null)); assertThat(output.getErr()).contains("WARN " + TEST_MESSAGE); assertThat(output.getErr()).contains("ERROR " + TEST_MESSAGE); @@ -115,8 +110,9 @@ class SystemStatusListenerTests { @Test void shouldRetrospectivePrintStatusOnStartAndDebugIsEnabled(CapturedOutput output) { - given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(new ErrorStatus(TEST_MESSAGE, null), - new WarnStatus(TEST_MESSAGE, null), new InfoStatus(TEST_MESSAGE, null))); + this.statusManager.add(new ErrorStatus(TEST_MESSAGE, null)); + this.statusManager.add(new WarnStatus(TEST_MESSAGE, null)); + this.statusManager.add(new InfoStatus(TEST_MESSAGE, null)); addStatus(true, () -> new InfoStatus(TEST_MESSAGE, null)); assertThat(output.getErr()).isEmpty(); assertThat(output.getOut()).contains("WARN " + TEST_MESSAGE); @@ -128,7 +124,7 @@ class SystemStatusListenerTests { void shouldNotRetrospectivePrintWhenStatusIsOutdated(CapturedOutput output) { ErrorStatus outdatedStatus = new ErrorStatus(TEST_MESSAGE, null); ReflectionTestUtils.setField(outdatedStatus, "timestamp", System.currentTimeMillis() - 300); - given(this.statusManager.getCopyOfStatusList()).willReturn(List.of(outdatedStatus)); + this.statusManager.add(outdatedStatus); addStatus(false, () -> new InfoStatus(TEST_MESSAGE, null)); assertThat(output.getOut()).isEmpty(); assertThat(output.getErr()).isEmpty(); @@ -136,10 +132,9 @@ class SystemStatusListenerTests { private void addStatus(boolean debug, Supplier statusFactory) { SystemStatusListener.addTo(this.loggerContext, debug); - ArgumentCaptor listener = ArgumentCaptor.forClass(StatusListener.class); - then(this.statusManager).should().add(listener.capture()); - assertThat(listener.getValue()).extracting("context").isSameAs(this.loggerContext); - listener.getValue().addStatusEvent(statusFactory.get()); + StatusListener listener = this.statusManager.getCopyOfStatusListenerList().get(0); + assertThat(listener).extracting("context").isSameAs(this.loggerContext); + listener.addStatusEvent(statusFactory.get()); } }