Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

defaultAnswer(CALLS_REAL_METHODS) for defaultMock #205

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.HashMap;
import java.util.Map;

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

Expand All @@ -35,15 +36,16 @@ public final class LoggerMock {

private static final Map<String, Logger> loggers = new HashMap<>();

private LoggerMock(){
private LoggerMock() {
// Utility classes
}

private static Logger createNewLoggerProxy(String name) {
return (Logger) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class<?>[]{Logger.class, ProxyMock.class},
new Class<?>[] {Logger.class, ProxyMock.class},
new MockInvocationHandler(name, () ->
mock(SimpleLogger.class, withSettings().spiedInstance(new SimpleLogger(name)).stubOnly())
mock(SimpleLogger.class, withSettings().spiedInstance(new SimpleLogger(name))
.defaultAnswer(CALLS_REAL_METHODS).stubOnly())
)
);
}
Expand All @@ -66,7 +68,7 @@ public static ILoggerFactory getLoggerFactory() {
* Set Mock for given Logger at current thread.
*
* @param klass Logger class
* @param mock Mock to assign to Logger
* @param mock Mock to assign to Logger
*/
public static void setMock(Class<?> klass, Logger mock) {
setMock(klass.getName(), mock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.simplify4u.slf4jmock.test;


import java.io.File;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -30,6 +32,7 @@
import org.simplify4u.slf4jmock.LoggerMock;
import org.slf4j.Logger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
Expand All @@ -40,12 +43,12 @@
@ExtendWith(MockitoExtension.class)
class TemplateExampleTest {

private static final String INFO_TEST_MESSAGE = "info log test message";
private static final String INFO_TEST_MESSAGE = "info log test message - ExampleTest";
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: {}";

static abstract class ExampleTestCommon {
abstract static class ExampleTestCommon {

@Mock
Logger logger;
Expand Down Expand Up @@ -92,6 +95,12 @@ void logInfoShouldBeLogged() {
// then
verify(logger).info(INFO_TEST_MESSAGE);
verifyNoMoreInteractions(logger);

File logFile = new File("target/slf4j-simpleLogger.log");
if (logFile.exists()) {
assertThat(logFile)
.content().doesNotContain(INFO_TEST_MESSAGE);
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.simplify4u.slf4jmock.test;

import java.io.File;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -25,6 +27,7 @@
import org.simplify4u.slf4jmock.SimpleLogger;
import org.slf4j.Logger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;

Expand All @@ -45,10 +48,13 @@ class TemplateInitializedSpyLoggerTest {
@Test
void testSpy() {

sut.methodWithLogInfo("Info message");
sut.methodWithLogInfo("Info message - InitializedSpyLoggerTest");

Mockito.verify(logger1).info("Info message");
Mockito.verify(logger1).info("Info message - InitializedSpyLoggerTest");
Mockito.verify(logger2, never()).info(anyString());

assertThat(new File("target/slf4j-simpleLogger.log"))
.content().contains("Info message - InitializedSpyLoggerTest");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.simplify4u.slf4jmock.test;

import java.io.File;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -24,6 +26,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.Logger;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(MockitoExtension.class)
class TemplateSpyLoggerTest {
Expand All @@ -37,10 +40,13 @@ class TemplateSpyLoggerTest {
@Test
void testSpy() {

sut.methodWithLogInfo("Info message");
sut.methodWithLogInfo("Info message - SpyLoggerTest");

Mockito.verify(logger).info("Info message");
Mockito.verify(logger).info("Info message - SpyLoggerTest");
Mockito.verifyNoMoreInteractions(logger);

assertThat(new File("target/slf4j-simpleLogger.log"))
.content().contains("Info message - SpyLoggerTest");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.simplify4u.slf4jmock.test;

import java.io.File;

import org.junit.jupiter.api.Test;
import org.slf4j.MDC;

Expand All @@ -28,8 +30,11 @@ class TemplateWithOutMocksTest {

@Test
void loggerTest() {
assertThatCode(() -> sut.methodWithLogInfo("Some message"))
assertThatCode(() -> sut.methodWithLogInfo("Some message - WithOutMocksTest"))
.doesNotThrowAnyException();

assertThat(new File("target/slf4j-simpleLogger.log"))
.content().contains("Some message - WithOutMocksTest");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@


org.slf4j.simpleLogger.defaultLogLevel=debug
org.slf4j.simpleLogger.logFile=target/slf4j-simpleLogger.log