Skip to content
This repository has been archived by the owner on Jan 5, 2025. It is now read-only.

Commit

Permalink
Custom name for a logger mock
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalijr2 committed Jul 16, 2024
1 parent 6131185 commit 4142db8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public MockitoLoggerFinder() {
*/
@Override
public Logger getLogger(String name, Module module) {
return loggers.computeIfAbsent(name, key -> mock(Logger.class));
return loggers.computeIfAbsent(name, key -> mock(Logger.class, "Mock for logger " + key));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import static org.hamcrest.collection.IsMapWithSize.aMapWithSize;
import static org.hamcrest.core.Is.isA;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.hamcrest.object.HasToString.hasToString;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.Mockito.mock;

Expand All @@ -30,7 +32,8 @@ void createLogger() {

// then
assertAll("Logger was created", () -> assertThat("size", loggers, aMapWithSize(1)),
() -> assertThat("entry", loggers, hasEntry(equalTo("test"), isA(Logger.class))));
() -> assertThat("entry", loggers, hasEntry(equalTo("test"), isA(Logger.class))),
() -> assertThat(loggers.values().iterator().next(), hasToString("Mock for logger test")));
}

@DisplayName("Reuse existing logger")
Expand All @@ -45,7 +48,8 @@ void reuseExistingLogger() {

// then
assertAll("Logger was reused", () -> assertThat("size", loggers, aMapWithSize(1)),
() -> assertThat("entry", loggers, hasEntry(equalTo("test"), isA(Logger.class))));
() -> assertThat("entry", loggers, hasEntry(equalTo("test"), isA(Logger.class))),
() -> assertThat(loggers.values().iterator().next(), hasToString(startsWith("Mock for Logger, hashCode:"))));
}

}

0 comments on commit 4142db8

Please sign in to comment.