diff --git a/approvaltests-tests/src/test/java/org/approvaltests/OptionsTest.java b/approvaltests-tests/src/test/java/org/approvaltests/OptionsTest.java index 87e7be4f..ae1ba95b 100644 --- a/approvaltests-tests/src/test/java/org/approvaltests/OptionsTest.java +++ b/approvaltests-tests/src/test/java/org/approvaltests/OptionsTest.java @@ -1,7 +1,6 @@ package org.approvaltests; import com.spun.util.ArrayUtils; -import com.spun.util.introspection.Caller; import org.approvaltests.approvers.FileApprover; import org.approvaltests.awt.AwtApprovals; import org.approvaltests.combinations.CombinationApprovals; @@ -10,6 +9,9 @@ import org.approvaltests.core.Options; import org.approvaltests.core.VerifyResult; import org.approvaltests.reporters.FirstWorkingReporter; +import org.approvaltests.reporters.Junit4Reporter; +import org.approvaltests.reporters.Junit5Reporter; +import org.approvaltests.reporters.MultiReporter; import org.approvaltests.reporters.UseReporter; import org.approvaltests.reporters.UseReporterTest; import org.approvaltests.velocity.VelocityApprovals; @@ -195,4 +197,17 @@ void verifyCustomComparator() { Approvals.verify("The approval file is empty", new Options().withComparator((a, b) -> VerifyResult.SUCCESS)); } + @Test + void testAddReporter() + { + Options options = new Options().withReporter(new Junit5Reporter()).withReporter(new Junit4Reporter()); + ApprovalFailureReporter reporter = options.getReporter(); + assertTrue(reporter instanceof Junit4Reporter); + Options options2 = new Options().withReporter(new MultiReporter(new Junit5Reporter(), new Junit4Reporter())); + ApprovalFailureReporter reporter2 = options2.getReporter(); + assertEquals(reporter2.toString(), "Junit5Reporter, Junit4Reporter"); + Options options3 = new Options().withReporter(new Junit5Reporter()).andReporter(new Junit4Reporter()); + ApprovalFailureReporter reporter3 = options3.getReporter(); + assertEquals(reporter3.toString(), "Junit5Reporter, Junit4Reporter"); + } } diff --git a/approvaltests/src/main/java/org/approvaltests/core/Options.java b/approvaltests/src/main/java/org/approvaltests/core/Options.java index 239fb3c8..1f7049ff 100644 --- a/approvaltests/src/main/java/org/approvaltests/core/Options.java +++ b/approvaltests/src/main/java/org/approvaltests/core/Options.java @@ -8,6 +8,7 @@ import org.approvaltests.inline.InlineOptions; import org.approvaltests.namer.ApprovalNamer; import org.approvaltests.namer.NamerWrapper; +import org.approvaltests.reporters.MultiReporter; import org.approvaltests.scrubbers.NoOpScrubber; import org.approvaltests.writers.ApprovalWriterFactory; import org.approvaltests.writers.DefaultApprovalWriterFactory; @@ -61,6 +62,10 @@ public Options withReporter(ApprovalFailureReporter reporter) { return new Options(fields, Fields.REPORTER, reporter); } + public Options andReporter(ApprovalFailureReporter reporter) + { + return this.withReporter(new MultiReporter(this.getReporter(), reporter)); + } public Function2 getComparator() { return ArrayUtils.getOrElse(fields, Fields.COMPARATOR, () -> FileApprover::approveTextFile); diff --git a/approvaltests/src/main/java/org/approvaltests/reporters/MultiReporter.java b/approvaltests/src/main/java/org/approvaltests/reporters/MultiReporter.java index 239d483c..764cf9d9 100644 --- a/approvaltests/src/main/java/org/approvaltests/reporters/MultiReporter.java +++ b/approvaltests/src/main/java/org/approvaltests/reporters/MultiReporter.java @@ -1,6 +1,7 @@ package org.approvaltests.reporters; import org.approvaltests.core.ApprovalFailureReporter; +import org.lambda.query.Queryable; import java.util.ArrayList; import java.util.Arrays; @@ -40,4 +41,9 @@ public ApprovalFailureReporter[] getReporters() { return reporters.toArray(new ApprovalFailureReporter[0]); } + @Override + public String toString() + { + return Queryable.as(reporters.stream()).join(", ", r -> r.getClass().getSimpleName()); + } }