Skip to content

Commit

Permalink
Add an extentions and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalijr2 committed Sep 23, 2024
1 parent d1353b4 commit daee454
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/java/com/ocarlsen/test/MockLoggerExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.ocarlsen.test;

import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.LoggerFactory;

public class MockLoggerExtension implements AfterEachCallback, BeforeEachCallback {

private final MockLoggerFactory loggerFactory;

static MockLoggerFactory getMockLoggerFactory() {
var loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof MockLoggerFactory) {
return (MockLoggerFactory) loggerFactory;
} else {
throw new ExtensionConfigurationException("The logger factory is not a MockLoggerFactory");
}
}

public MockLoggerExtension() {
this(getMockLoggerFactory());
}

MockLoggerExtension(MockLoggerFactory loggerFactory) {
this.loggerFactory = loggerFactory;
}

@Override
public void afterEach(ExtensionContext context) throws Exception {
loggerFactory.cleanAndResetMockLoggers();
}

@Override
public void beforeEach(ExtensionContext context) throws Exception {
loggerFactory.cleanAndResetMockLoggers();
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/ocarlsen/test/MockLoggerFactory.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.ocarlsen.test;

import java.util.ArrayList;
import java.util.List;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;

/**
* http://www.slf4j.org/faq.html#slf4j_compatible
Expand All @@ -19,4 +23,11 @@ public class MockLoggerFactory implements ILoggerFactory {
public Logger getLogger(final String name) {
return nameLoggerMap.computeIfAbsent(name, key -> mock(Logger.class));
}

void cleanAndResetMockLoggers() {
nameLoggerMap.forEach((loggerName, logger) -> {
clearInvocations(logger);
reset(logger);
});
}
}
85 changes: 85 additions & 0 deletions src/test/java/com/ocarlsen/test/MockLoggerExtensionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.ocarlsen.test;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;

import java.lang.System.LoggerFinder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.ExtensionConfigurationException;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@ExtendWith(MockitoExtension.class)
class MockLoggerExtensionTest {

@Mock
private ExtensionContext extensionContext;
@Mock
private org.junit.platform.commons.logging.Logger extensionLogger;

private MockLoggerExtension extension;
private Logger firstLogger;
private Logger secondLogger;

@BeforeEach
void setUp() {
var loggerFactory = new MockLoggerFactory();
extension = new MockLoggerExtension(loggerFactory);
firstLogger = loggerFactory.getLogger("first");
secondLogger = loggerFactory.getLogger("second");
}

@DisplayName("Initialize a logger finder on \"before all\" step")
@Test
void initLoggerFinderOnBeforeAll() {
assertDoesNotThrow(() -> new MockLoggerExtension());
}

@DisplayName("Unknown logger finder")
@Test
void unknownLoggerFinder() {
try (var loggerFactory = mockStatic(LoggerFactory.class)) {
loggerFactory.when(LoggerFactory::getILoggerFactory).thenReturn(mock(ILoggerFactory.class));

var exception = assertThrows(ExtensionConfigurationException.class, MockLoggerExtension::getMockLoggerFactory);

assertEquals("The logger factory is not a MockLoggerFactory", exception.getMessage());
}
}

@DisplayName("Clean and reset loggers after each test")
@Test
void resetLoggersAfterEachTest() {
firstLogger.info("test message");
secondLogger.info("another test message");

assertDoesNotThrow(() -> extension.afterEach(extensionContext));

verifyNoInteractions(extensionContext);
verifyNoInteractions(firstLogger);
verifyNoInteractions(secondLogger);
}

@DisplayName("Clean and reset loggers before each test")
@Test
void resetLoggersBeforeEachTest() {
firstLogger.info("test message");
secondLogger.info("another test message");

assertDoesNotThrow(() -> extension.beforeEach(extensionContext));

verifyNoInteractions(extensionContext);
verifyNoInteractions(firstLogger);
verifyNoInteractions(secondLogger);
}
}

0 comments on commit daee454

Please sign in to comment.