From 194f256d8be66fa233b9cd9fb510f7ca03277922 Mon Sep 17 00:00:00 2001
From: Jack Matthews <6348088+jackmatt2@users.noreply.github.com>
Date: Sun, 29 Jan 2023 20:13:21 +1100
Subject: [PATCH] #patch: #145 make updating snapshots simpler (#146)
- Allow snapshots to be updated by toggling `update-snapshot` property in `snapshot.properties`.
- Deprecate the passing of system property `-PupdateSnapshot`.
- Add some API deprecation logs for V5
---
README.md | 55 ++-----
.../com/origin/snapshots/SnapshotContext.java | 15 +-
.../au/com/origin/snapshots/SnapshotFile.java | 12 +-
.../PropertyResolvingSnapshotConfig.java | 33 ++++
.../snapshots/config/SnapshotConfig.java | 2 +-
.../snapshots/logging/LoggingHelper.java | 11 ++
.../snapshots/UpdateSnapshotPropertyTest.java | 8 +-
.../origin/snapshots/UpdateSnapshotTest.java | 153 ++++++++++++++++++
.../snapshots/junit4/SnapshotRunner.java | 6 +
.../src/test/resources/snapshot.properties | 3 +-
.../snapshots/junit5/SnapshotExtension.java | 9 +-
.../SnapshotContextExtensionUsedTest.snap | 32 ----
.../src/test/resources/snapshot.properties | 3 +-
.../src/test/resources/snapshot.properties | 3 +-
.../spock/SnapshotMethodInterceptor.groovy | 6 +-
.../src/test/resources/snapshot.properties | 3 +-
16 files changed, 259 insertions(+), 95 deletions(-)
create mode 100644 java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/logging/LoggingHelper.java
create mode 100644 java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotTest.java
delete mode 100644 java-snapshot-testing-junit5/src/test/java/au/com/origin/snapshots/__snapshots__/SnapshotContextExtensionUsedTest.snap
diff --git a/README.md b/README.md
index 6d12547..e9a32b5 100644
--- a/README.md
+++ b/README.md
@@ -56,6 +56,7 @@ reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
snapshot-dir=__snapshots__
output-dir=src/test/java
ci-env-var=CI
+update-snapshot=none
```
3. Enable snapshot testing and write your first test
@@ -447,17 +448,18 @@ Often your IDE has an excellent file comparison tool.
This file allows you to conveniently setup global defaults
-| key | Description |
-|------------------|----------------------------------------------------------------------------------------------------------------------------------------|
-|serializer | Class name of the [serializer](#supplying-a-custom-snapshotserializer), default serializer |
-|serializer.{name} | Class name of the [serializer](#supplying-a-custom-snapshotserializer), accessible via `.serializer("{name}")` |
-|comparator | Class name of the [comparator](#supplying-a-custom-snapshotcomparator) |
-|comparator.{name} | Class name of the [comparator](#supplying-a-custom-snapshotcomparator), accessible via `.comparator("{name}")` |
-|reporters | Comma separated list of class names to use as [reporters](#supplying-a-custom-snapshotreporter) |
-|reporters.{name} | Comma separated list of class names to use as [reporters](#supplying-a-custom-snapshotreporter), accessible via `.reporters("{name}")` |
-|snapshot-dir | Name of sub-folder holding your snapshots |
-|output-dir | Base directory of your test files (although it can be a different directory if you want) |
-|ci-env-var | Name of environment variable used to detect if we are running on a Build Server |
+| key | Description |
+|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
+|serializer | Class name of the [serializer](#supplying-a-custom-snapshotserializer), default serializer |
+|serializer.{name} | Class name of the [serializer](#supplying-a-custom-snapshotserializer), accessible via `.serializer("{name}")` |
+|comparator | Class name of the [comparator](#supplying-a-custom-snapshotcomparator) |
+|comparator.{name} | Class name of the [comparator](#supplying-a-custom-snapshotcomparator), accessible via `.comparator("{name}")` |
+|reporters | Comma separated list of class names to use as [reporters](#supplying-a-custom-snapshotreporter) |
+|reporters.{name} | Comma separated list of class names to use as [reporters](#supplying-a-custom-snapshotreporter), accessible via `.reporters("{name}")` |
+|snapshot-dir | Name of sub-folder holding your snapshots |
+|output-dir | Base directory of your test files (although it can be a different directory if you want) |
+|ci-env-var | Name of environment variable used to detect if we are running on a Build Server |
+|update-snapshot | Similar to `--updateSnapshot` in [Jest](https://jestjs.io/docs/en/snapshot-testing#updating-snapshots)
[all]=update all snapsohts
[none]=update no snapshots
[MyTest1,MyTest2]=update snapshots in these classes only
*Note: must be set to [none] on CI |
For example:
@@ -471,6 +473,7 @@ reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
snapshot-dir=__snapshots__
output-dir=src/test/java
ci-env-var=CI
+update-snapshot=none
```
## Parameterized tests
@@ -784,36 +787,6 @@ public class JUnit5ResolutionHierarchyExample {
}
```
-## Automatically overwriting snapshots via `-PupdateSnapshot=filter`
-
-Often - after analysing each snapshot and verifying it is correct, you will need to override the existing
-snapshots.
-
-Note that you may need to do some Gradle trickery to make this visible to your actual tests
-
-```groovy
-test {
- systemProperty "updateSnapshot", project.getProperty("updateSnapshot")
-}
-```
-
-Instead of deleting or manually modifying each snapshot you can pass `-PupdateSnapshot` which is equivalent to
-the `--updateSnapshot` flag in [Jest](https://jestjs.io/docs/en/snapshot-testing#updating-snapshots)
-
-#### Update all snapshots automatically
-
-```
--PupdateSnapshot
-```
-
-#### Update selected snapshots only using `filter`
-
-pass the class names you want to update to `filter`
-
-```
--PupdateSnapshot=UserService,PermissionRepository
-```
-
# Troubleshooting
**I'm seeing this error in my logs**
diff --git a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotContext.java b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotContext.java
index fe113a5..ae0398e 100644
--- a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotContext.java
+++ b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotContext.java
@@ -4,6 +4,7 @@
import au.com.origin.snapshots.comparators.SnapshotComparator;
import au.com.origin.snapshots.config.SnapshotConfig;
import au.com.origin.snapshots.exceptions.ReservedWordException;
+import au.com.origin.snapshots.exceptions.SnapshotExtensionException;
import au.com.origin.snapshots.exceptions.SnapshotMatchException;
import au.com.origin.snapshots.reporters.SnapshotReporter;
import au.com.origin.snapshots.serializers.SnapshotSerializer;
@@ -118,6 +119,12 @@ public void toMatchSnapshot() {
}
private boolean shouldUpdateSnapshot() {
+ if (snapshotConfig.updateSnapshot().isPresent() && snapshotConfig.isCI()) {
+ throw new SnapshotExtensionException(
+ "isCI=true & update-snapshot="
+ + snapshotConfig.updateSnapshot()
+ + ". Updating snapshots on CI is not allowed");
+ }
if (snapshotConfig.updateSnapshot().isPresent()) {
return resolveSnapshotIdentifier().contains(snapshotConfig.updateSnapshot().get());
} else {
@@ -126,9 +133,11 @@ private boolean shouldUpdateSnapshot() {
}
private Snapshot getRawSnapshot(Collection rawSnapshots) {
- for (Snapshot rawSnapshot : rawSnapshots) {
- if (rawSnapshot.getIdentifier().equals(resolveSnapshotIdentifier())) {
- return rawSnapshot;
+ synchronized (rawSnapshots) {
+ for (Snapshot rawSnapshot : rawSnapshots) {
+ if (rawSnapshot.getIdentifier().equals(resolveSnapshotIdentifier())) {
+ return rawSnapshot;
+ }
}
}
return null;
diff --git a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotFile.java b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotFile.java
index a3d53f9..4a19972 100644
--- a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotFile.java
+++ b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/SnapshotFile.java
@@ -108,11 +108,13 @@ public synchronized File createFileIfNotExists(String filename) {
return path.toFile();
}
- public synchronized void pushSnapshot(Snapshot snapshot) {
- snapshots.add(snapshot);
- TreeSet rawSnapshots =
- snapshots.stream().map(Snapshot::raw).collect(Collectors.toCollection(TreeSet::new));
- updateFile(this.fileName, rawSnapshots);
+ public void pushSnapshot(Snapshot snapshot) {
+ synchronized (snapshots) {
+ snapshots.add(snapshot);
+ TreeSet rawSnapshots =
+ snapshots.stream().map(Snapshot::raw).collect(Collectors.toCollection(TreeSet::new));
+ updateFile(this.fileName, rawSnapshots);
+ }
}
public synchronized void pushDebugSnapshot(Snapshot snapshot) {
diff --git a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/PropertyResolvingSnapshotConfig.java b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/PropertyResolvingSnapshotConfig.java
index b9d480c..3acab87 100644
--- a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/PropertyResolvingSnapshotConfig.java
+++ b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/PropertyResolvingSnapshotConfig.java
@@ -2,10 +2,15 @@
import au.com.origin.snapshots.SnapshotProperties;
import au.com.origin.snapshots.comparators.SnapshotComparator;
+import au.com.origin.snapshots.exceptions.MissingSnapshotPropertiesKeyException;
+import au.com.origin.snapshots.logging.LoggingHelper;
import au.com.origin.snapshots.reporters.SnapshotReporter;
import au.com.origin.snapshots.serializers.SnapshotSerializer;
import java.util.List;
+import java.util.Optional;
+import lombok.extern.slf4j.Slf4j;
+@Slf4j
public class PropertyResolvingSnapshotConfig implements SnapshotConfig {
@Override
@@ -18,6 +23,34 @@ public String getSnapshotDir() {
return SnapshotProperties.getOrThrow("snapshot-dir");
}
+ @Override
+ public Optional updateSnapshot() {
+ // This was the original way to update snapshots
+ Optional legacyFlag =
+ Optional.ofNullable(System.getProperty(JVM_UPDATE_SNAPSHOTS_PARAMETER));
+ if (legacyFlag.isPresent()) {
+ LoggingHelper.deprecatedV5(
+ log,
+ "Passing -PupdateSnapshot will be removed in a future release. Consider using snapshot.properties 'update-snapshot' toggle instead");
+ return legacyFlag;
+ }
+
+ try {
+ String updateSnapshot = SnapshotProperties.getOrThrow("update-snapshot");
+ if ("all".equals(updateSnapshot)) {
+ return Optional.of("");
+ } else if ("none".equals(updateSnapshot)) {
+ return Optional.empty();
+ }
+ return Optional.of(updateSnapshot);
+ } catch (MissingSnapshotPropertiesKeyException ex) {
+ LoggingHelper.deprecatedV5(
+ log,
+ "You do not have 'update-snapshot=none' defined in your snapshot.properties - consider adding it now");
+ return Optional.empty();
+ }
+ }
+
@Override
public SnapshotSerializer getSerializer() {
return SnapshotProperties.getInstance("serializer");
diff --git a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/SnapshotConfig.java b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/SnapshotConfig.java
index d51e9eb..45bdeae 100644
--- a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/SnapshotConfig.java
+++ b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/config/SnapshotConfig.java
@@ -13,7 +13,7 @@
* library
*/
public interface SnapshotConfig {
- String JVM_UPDATE_SNAPSHOTS_PARAMETER = "updateSnapshot";
+ @Deprecated String JVM_UPDATE_SNAPSHOTS_PARAMETER = "updateSnapshot";
/**
* The base directory where files get written (excluding package directories) default:
diff --git a/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/logging/LoggingHelper.java b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/logging/LoggingHelper.java
new file mode 100644
index 0000000..774d6c0
--- /dev/null
+++ b/java-snapshot-testing-core/src/main/java/au/com/origin/snapshots/logging/LoggingHelper.java
@@ -0,0 +1,11 @@
+package au.com.origin.snapshots.logging;
+
+import org.slf4j.Logger;
+
+public class LoggingHelper {
+
+ public static void deprecatedV5(Logger log, String message) {
+ log.warn(
+ "Deprecation Warning:\n " + message + "\n\nThis feature will be removed in version 5.X");
+ }
+}
diff --git a/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotPropertyTest.java b/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotPropertyTest.java
index be9b075..c6f2b92 100644
--- a/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotPropertyTest.java
+++ b/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotPropertyTest.java
@@ -16,6 +16,7 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
+@Deprecated
@ExtendWith(MockitoExtension.class)
public class UpdateSnapshotPropertyTest {
@@ -72,13 +73,6 @@ void shouldUpdateSnapshot(TestInfo testInfo) throws IOException {
+ "]");
}
- @Disabled
- @Test
- void shouldUpdateAllSnapshots() throws IOException {
- System.setProperty(SnapshotConfig.JVM_UPDATE_SNAPSHOTS_PARAMETER, "");
- // FIXME
- }
-
@Test
void shouldNotUpdateSnapshot(TestInfo testInfo) {
SnapshotVerifier snapshotVerifier =
diff --git a/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotTest.java b/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotTest.java
new file mode 100644
index 0000000..b999284
--- /dev/null
+++ b/java-snapshot-testing-core/src/test/java/au/com/origin/snapshots/UpdateSnapshotTest.java
@@ -0,0 +1,153 @@
+package au.com.origin.snapshots;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import au.com.origin.snapshots.config.BaseSnapshotConfig;
+import au.com.origin.snapshots.config.SnapshotConfig;
+import au.com.origin.snapshots.exceptions.SnapshotMatchException;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Optional;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+@ExtendWith(MockitoExtension.class)
+public class UpdateSnapshotTest {
+
+ @BeforeEach
+ public void beforeEach() throws Exception {
+ File file =
+ new File("src/test/java/au/com/origin/snapshots/__snapshots__/UpdateSnapshotTest.snap");
+ String content =
+ "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateAllSnapshots=[\n"
+ + "OLD\n"
+ + "]\n"
+ + "\n"
+ + "\n"
+ + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateClassNameSnapshots=[\n"
+ + "OLD\n"
+ + "]\n"
+ + "\n"
+ + "\n"
+ + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateNoSnapshots=[\n"
+ + "OLD\n"
+ + "]";
+ Path parentDir = file.getParentFile().toPath();
+ if (!Files.exists(parentDir)) {
+ Files.createDirectories(parentDir);
+ }
+ Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8));
+ }
+
+ @Test
+ void canUpdateAllSnapshots(TestInfo testInfo) throws IOException {
+ SnapshotConfig config =
+ new BaseSnapshotConfig() {
+ @Override
+ public Optional updateSnapshot() {
+ return Optional.of("");
+ }
+ };
+ SnapshotVerifier snapshotVerifier =
+ new SnapshotVerifier(config, testInfo.getTestClass().get(), false);
+ Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
+ expect.toMatchSnapshot("NEW");
+ snapshotVerifier.validateSnapshots();
+
+ String content =
+ new String(
+ Files.readAllBytes(
+ Paths.get(
+ "src/test/java/au/com/origin/snapshots/__snapshots__/UpdateSnapshotTest.snap")),
+ StandardCharsets.UTF_8);
+ Assertions.assertThat(content)
+ .isEqualTo(
+ "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateAllSnapshots=[\n"
+ + "NEW\n"
+ + "]\n"
+ + "\n"
+ + "\n"
+ + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateClassNameSnapshots=[\n"
+ + "OLD\n"
+ + "]\n"
+ + "\n"
+ + "\n"
+ + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateNoSnapshots=[\n"
+ + "OLD\n"
+ + "]");
+ }
+
+ @Test
+ void canUpdateNoSnapshots(TestInfo testInfo) {
+ SnapshotConfig config =
+ new BaseSnapshotConfig() {
+ @Override
+ public Optional updateSnapshot() {
+ return Optional.empty();
+ }
+ };
+ SnapshotVerifier snapshotVerifier =
+ new SnapshotVerifier(config, testInfo.getTestClass().get(), false);
+ Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
+ assertThrows(SnapshotMatchException.class, () -> expect.toMatchSnapshot("FOOBAR"));
+ }
+
+ @Test
+ public void canUpdateNewSnapshots() {
+ SnapshotConfig config =
+ new BaseSnapshotConfig() {
+ @Override
+ public Optional updateSnapshot() {
+ return Optional.of("new");
+ }
+ };
+
+ // TODO Pending Implementation
+ }
+
+ @Test
+ public void canUpdateClassNameSnapshots(TestInfo testInfo) throws IOException {
+ SnapshotConfig config =
+ new BaseSnapshotConfig() {
+ @Override
+ public Optional updateSnapshot() {
+ return Optional.of("UpdateSnapshotTest");
+ }
+ };
+ SnapshotVerifier snapshotVerifier =
+ new SnapshotVerifier(config, testInfo.getTestClass().get(), false);
+ Expect expect = Expect.of(snapshotVerifier, testInfo.getTestMethod().get());
+ expect.toMatchSnapshot("NEW");
+ snapshotVerifier.validateSnapshots();
+
+ String content =
+ new String(
+ Files.readAllBytes(
+ Paths.get(
+ "src/test/java/au/com/origin/snapshots/__snapshots__/UpdateSnapshotTest.snap")),
+ StandardCharsets.UTF_8);
+ Assertions.assertThat(content)
+ .isEqualTo(
+ "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateAllSnapshots=[\n"
+ + "OLD\n"
+ + "]\n"
+ + "\n"
+ + "\n"
+ + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateClassNameSnapshots=[\n"
+ + "NEW\n"
+ + "]\n"
+ + "\n"
+ + "\n"
+ + "au.com.origin.snapshots.UpdateSnapshotTest.canUpdateNoSnapshots=[\n"
+ + "OLD\n"
+ + "]");
+ }
+}
diff --git a/java-snapshot-testing-junit4/src/main/java/au/com/origin/snapshots/junit4/SnapshotRunner.java b/java-snapshot-testing-junit4/src/main/java/au/com/origin/snapshots/junit4/SnapshotRunner.java
index c3b0530..dfc954e 100644
--- a/java-snapshot-testing-junit4/src/main/java/au/com/origin/snapshots/junit4/SnapshotRunner.java
+++ b/java-snapshot-testing-junit4/src/main/java/au/com/origin/snapshots/junit4/SnapshotRunner.java
@@ -1,7 +1,9 @@
package au.com.origin.snapshots.junit4;
import au.com.origin.snapshots.SnapshotVerifier;
+import au.com.origin.snapshots.logging.LoggingHelper;
import java.util.List;
+import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
@@ -29,6 +31,7 @@
* Loosely based on:
* https://stackoverflow.com/questions/27745691/how-to-combine-runwith-with-runwithparameterized-class
*/
+@Slf4j
public class SnapshotRunner extends BlockJUnit4ClassRunner {
SnapshotVerifier snapshotVerifier;
@@ -46,6 +49,9 @@ protected Statement methodInvoker(FrameworkMethod method, Object test) {
helpers.injectExpectInstanceVariable(snapshotVerifier, method.getMethod(), test);
boolean shouldInjectMethodArgument = helpers.hasExpectArgument(method);
if (shouldInjectMethodArgument) {
+ LoggingHelper.deprecatedV5(
+ log,
+ "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.");
return helpers.injectExpectMethodArgument(snapshotVerifier, method, test);
}
}
diff --git a/java-snapshot-testing-junit4/src/test/resources/snapshot.properties b/java-snapshot-testing-junit4/src/test/resources/snapshot.properties
index 0fbea34..4aeb8c2 100644
--- a/java-snapshot-testing-junit4/src/test/resources/snapshot.properties
+++ b/java-snapshot-testing-junit4/src/test/resources/snapshot.properties
@@ -3,4 +3,5 @@ comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
snapshot-dir=__snapshots__
output-dir=src/test/java
-ci-env-var=CI
\ No newline at end of file
+ci-env-var=CI
+update-snapshot=none
\ No newline at end of file
diff --git a/java-snapshot-testing-junit5/src/main/java/au/com/origin/snapshots/junit5/SnapshotExtension.java b/java-snapshot-testing-junit5/src/main/java/au/com/origin/snapshots/junit5/SnapshotExtension.java
index c3c8b2e..aba8b32 100644
--- a/java-snapshot-testing-junit5/src/main/java/au/com/origin/snapshots/junit5/SnapshotExtension.java
+++ b/java-snapshot-testing-junit5/src/main/java/au/com/origin/snapshots/junit5/SnapshotExtension.java
@@ -6,6 +6,7 @@
import au.com.origin.snapshots.config.SnapshotConfig;
import au.com.origin.snapshots.config.SnapshotConfigInjector;
import au.com.origin.snapshots.exceptions.SnapshotMatchException;
+import au.com.origin.snapshots.logging.LoggingHelper;
import java.lang.reflect.Field;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
@@ -80,7 +81,13 @@ private boolean shouldFailOnOrphans(ExtensionContext context) {
public boolean supportsParameter(
ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
- return parameterContext.getParameter().getType() == Expect.class;
+ boolean supports = parameterContext.getParameter().getType() == Expect.class;
+ if (supports) {
+ LoggingHelper.deprecatedV5(
+ log,
+ "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.");
+ }
+ return supports;
}
@Override
diff --git a/java-snapshot-testing-junit5/src/test/java/au/com/origin/snapshots/__snapshots__/SnapshotContextExtensionUsedTest.snap b/java-snapshot-testing-junit5/src/test/java/au/com/origin/snapshots/__snapshots__/SnapshotContextExtensionUsedTest.snap
deleted file mode 100644
index 4d055ee..0000000
--- a/java-snapshot-testing-junit5/src/test/java/au/com/origin/snapshots/__snapshots__/SnapshotContextExtensionUsedTest.snap
+++ /dev/null
@@ -1,32 +0,0 @@
-au.com.origin.snapshots.SnapshotExtensionUsedTest.shouldUseExtension=[
-Hello World
-]
-
-
-au.com.origin.snapshots.SnapshotExtensionUsedTest.shouldUseExtensionAgain=[
-Hello World
-Hello World Again
-]
-
-
-au.com.origin.snapshots.SnapshotExtensionUsedTest.shouldUseExtensionAgainViaInstanceVariable=[
-Hello World
-Hello World Again
-]
-
-
-au.com.origin.snapshots.SnapshotExtensionUsedTest.shouldUseExtensionViaInstanceVariable=[
-Hello World
-]
-
-
-hello_world=[
-Hello World
-Hello World
-]
-
-
-hello_world_2=[
-Hello World
-Hello World Again
-]
\ No newline at end of file
diff --git a/java-snapshot-testing-junit5/src/test/resources/snapshot.properties b/java-snapshot-testing-junit5/src/test/resources/snapshot.properties
index a3004aa..3dbdab5 100644
--- a/java-snapshot-testing-junit5/src/test/resources/snapshot.properties
+++ b/java-snapshot-testing-junit5/src/test/resources/snapshot.properties
@@ -4,4 +4,5 @@ comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
snapshot-dir=__snapshots__
output-dir=src/test/java
-ci-env-var=CI
\ No newline at end of file
+ci-env-var=CI
+update-snapshot=none
\ No newline at end of file
diff --git a/java-snapshot-testing-plugin-jackson/src/test/resources/snapshot.properties b/java-snapshot-testing-plugin-jackson/src/test/resources/snapshot.properties
index 39cdf5d..141beb8 100644
--- a/java-snapshot-testing-plugin-jackson/src/test/resources/snapshot.properties
+++ b/java-snapshot-testing-plugin-jackson/src/test/resources/snapshot.properties
@@ -4,4 +4,5 @@ comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
snapshot-dir=__snapshots__
output-dir=src/test/java
-ci-env-var=CI
\ No newline at end of file
+ci-env-var=CI
+update-snapshot=none
\ No newline at end of file
diff --git a/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotMethodInterceptor.groovy b/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotMethodInterceptor.groovy
index e26e8c4..ea6239d 100644
--- a/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotMethodInterceptor.groovy
+++ b/java-snapshot-testing-spock/src/main/groovy/au/com/origin/snapshots/spock/SnapshotMethodInterceptor.groovy
@@ -2,6 +2,9 @@ package au.com.origin.snapshots.spock
import au.com.origin.snapshots.Expect
import au.com.origin.snapshots.SnapshotVerifier
+import au.com.origin.snapshots.logging.LoggingHelper
+import lombok.extern.slf4j.Slf4j
+import org.slf4j.LoggerFactory
import org.spockframework.runtime.extension.AbstractMethodInterceptor
import org.spockframework.runtime.extension.IMethodInterceptor
import org.spockframework.runtime.extension.IMethodInvocation
@@ -10,7 +13,7 @@ import java.lang.reflect.Method
// Based on this issue: https://github.com/spockframework/spock/issues/652
class SnapshotMethodInterceptor extends AbstractMethodInterceptor {
-
+ private log = LoggerFactory.getLogger( SnapshotMethodInterceptor.class )
private final SnapshotVerifier snapshotVerifier;
SnapshotMethodInterceptor(SnapshotVerifier snapshotVerifier) {
@@ -29,6 +32,7 @@ class SnapshotMethodInterceptor extends AbstractMethodInterceptor {
}
invocation.method.reflection.parameterTypes.eachWithIndex { type, i ->
if (Expect.class == type) {
+ LoggingHelper.deprecatedV5(log, "Injecting 'Expect' via method a argument is no longer recommended. Consider using instance variable injection instead.")
invocation.arguments[i] = new Expect(snapshotVerifier, invocation.feature.featureMethod.reflection)
}
}
diff --git a/java-snapshot-testing-spock/src/test/resources/snapshot.properties b/java-snapshot-testing-spock/src/test/resources/snapshot.properties
index a3f040e..0c9644a 100644
--- a/java-snapshot-testing-spock/src/test/resources/snapshot.properties
+++ b/java-snapshot-testing-spock/src/test/resources/snapshot.properties
@@ -3,4 +3,5 @@ comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
snapshot-dir=__snapshots__
output-dir=src/test/groovy
-ci-env-var=CI
\ No newline at end of file
+ci-env-var=CI
+update-snapshot=none
\ No newline at end of file