Skip to content

Commit

Permalink
Merge pull request #27 from s4u/mock-lenient
Browse files Browse the repository at this point in the history
Create mock as lenient
  • Loading branch information
slawekjaranowski authored Oct 13, 2020
2 parents 6da4add + 608be2c commit 8a9b03d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/simplify4u/sjf4jmock/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import org.mockito.Mockito;
import org.slf4j.ILoggerFactory;
Expand All @@ -40,16 +41,15 @@ public class LoggerFactory implements ILoggerFactory {
@Override
public Logger getLogger(String name) {
synchronized (loggers) {
return loggers.computeIfAbsent(name, this::createNewLoggerMock);
return loggers.computeIfAbsent(name, LoggerFactory::createNewLoggerMock);
}
}

@SuppressWarnings("java:S1312") // Loggers should be "private static final" and should share a naming convention
private Logger createNewLoggerMock(String name) {
private static Logger createNewLoggerMock(String name) {

LOGGER.debug("Create mock for logger: {}", name);

Logger mock = mock(Logger.class);
Logger mock = mock(Logger.class, withSettings().lenient());
when(mock.getName()).thenReturn(name);

new DelegateMockToSimpleLogger(name).delegate(mock);
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/simplify4u/sl4jmock/test/ExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.junit.MockitoJUnitRunner;
import org.simplify4u.sjf4jmock.LoggerMock;
import org.slf4j.Logger;

@RunWith(MockitoJUnitRunner.class)
public class ExampleTest {

private static final String INFO_TEST_MESSAGE = "info log test message";
private static final String WARN_TEST_MESSAGE = "warn log test message";
private static final String DEBUG_TEST_MESSAGE = "debug log test message";
private static final String DEBUG_TEST_FORMAT = "Debug: {}";

@InjectMocks
private Example sut;

@Before
public void setup() {
LoggerMock.clearInvocations();
sut = new Example();
}


Expand Down

0 comments on commit 8a9b03d

Please sign in to comment.