From 6291ed5a70511bc71d4462ae2ea16cefd7119cda Mon Sep 17 00:00:00 2001 From: Jens Wille Date: Wed, 30 Mar 2022 13:08:45 +0200 Subject: [PATCH] Suppress log messages when testing "strict" mode. (#192) Requires "inline mock making"; see https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#mockito-inline. --- metafix/build.gradle | 2 +- .../metafix/MetafixScriptTest.java | 22 ++++++++++++------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/metafix/build.gradle b/metafix/build.gradle index 4c9e6919..4445d5b6 100644 --- a/metafix/build.gradle +++ b/metafix/build.gradle @@ -31,7 +31,7 @@ dependencies { implementation "org.metafacture:metamorph:${versions.metafacture}" testImplementation "nl.jqno.equalsverifier:equalsverifier:${versions.equalsverifier}" - testImplementation "org.mockito:mockito-core:${versions.mockito}" + testImplementation "org.mockito:mockito-inline:${versions.mockito}" testImplementation "org.mockito:mockito-junit-jupiter:${versions.mockito}" } diff --git a/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java b/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java index c257568b..3b00e3ff 100644 --- a/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java +++ b/metafix/src/test/java/org/metafacture/metafix/MetafixScriptTest.java @@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import java.io.FileNotFoundException; @@ -390,14 +391,19 @@ public void shouldIncludeLocationAndTextInExecutionException() { ); } - private void assertStrictness(final Metafix.Strictness strictness, final String fixDef, final Consumer> out) { + private void assertStrictness(final Metafix.Strictness strictness, final String fixDef, final boolean stubLogging, final Consumer> out) { MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( "add_field('before', '')", fixDef, "add_field('after', '')" ), i -> { - i.setStrictness(strictness); + final Metafix.Strictness strictnessSpy = Mockito.spy(strictness); + i.setStrictness(strictnessSpy); + + if (stubLogging) { + Mockito.doNothing().when(strictnessSpy).log(Mockito.any(), Mockito.any()); + } i.startRecord("1"); i.literal("data", "foo"); @@ -418,13 +424,13 @@ private void assertStrictness(final Metafix.Strictness strictness, final String // TODO: Test logging statements } - private void assertStrictness(final Metafix.Strictness strictness, final Consumer> out) { - assertStrictness(strictness, "upcase('data')", out); + private void assertStrictness(final Metafix.Strictness strictness, final boolean stubLogging, final Consumer> out) { + assertStrictness(strictness, "upcase('data')", stubLogging, out); } @Test public void shouldSkipExpressionOnExecutionException() { - assertStrictness(Metafix.Strictness.EXPRESSION, o -> { + assertStrictness(Metafix.Strictness.EXPRESSION, true, o -> { o.get().startRecord("1"); o.get().literal("before", ""); o.get().literal("data", "FOO"); @@ -446,7 +452,7 @@ public void shouldSkipExpressionOnExecutionException() { @Test public void shouldSkipRecordOnExecutionException() { - assertStrictness(Metafix.Strictness.RECORD, o -> { + assertStrictness(Metafix.Strictness.RECORD, true, o -> { o.get().startRecord("1"); o.get().literal("before", ""); o.get().literal("data", "FOO"); @@ -464,7 +470,7 @@ public void shouldSkipRecordOnExecutionException() { @Test public void shouldAbortProcessOnExecutionException() { MetafixTestHelpers.assertExecutionException(IllegalStateException.class, "Expected String, got Array", () -> - assertStrictness(Metafix.Strictness.PROCESS, o -> { + assertStrictness(Metafix.Strictness.PROCESS, false, o -> { }) ); } @@ -472,7 +478,7 @@ public void shouldAbortProcessOnExecutionException() { @Test public void shouldAbortProcessOnProcessException() { MetafixTestHelpers.assertProcessException(IllegalArgumentException.class, "No enum constant org.metafacture.metafix.FixMethod.foo", () -> - assertStrictness(Metafix.Strictness.EXPRESSION, "foo()", o -> { + assertStrictness(Metafix.Strictness.EXPRESSION, "foo()", false, o -> { }) ); }