@@ -16,18 +16,26 @@ Then java-snapshot-testing might just be what you are looking for!
16
16
## Quick Start (Junit5 + Gradle example)
17
17
1 . Add test dependencies
18
18
``` groovy
19
+ // In this case we are using the JUnit5 testing framework
19
20
testImplementation 'io.github.origin-energy:java-snapshot-testing-junit5:2.+'
21
+
22
+ // Many will want to serialize into JSON. In this case you should also add the Jackson plugin
23
+ testImplementation 'io.github.origin-energy:java-snapshot-testing-plugin-jackson:2.+'
20
24
testImplementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
21
25
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.11.3'
22
26
testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.3'
23
27
testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.3'
28
+
29
+ // slf4j logging implementation if you don't already have one
30
+ testImplementation("org.slf4j:slf4j-simple:2.0.0-alpha0")
24
31
```
25
32
26
33
2 . Create ` snapshot.properties ` and configure your global settings. Be sure to set ` output-dir ` appropriately for you JVM language.
27
34
28
35
- /src/test/java/resources/snapshot.properties
29
36
``` text
30
37
serializer=au.com.origin.snapshots.serializers.ToStringSnapshotSerializer
38
+ serializer.json=au.com.origin.snapshots.serializers.DeterministicJacksonSnapshotSerializer
31
39
comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator
32
40
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
33
41
snapshot-dir=__snapshots__
@@ -44,6 +52,17 @@ public class MyFirstSnapshotTest {
44
52
public void helloWorldTest () {
45
53
expect(" Hello World" ). toMatchSnapshot();
46
54
}
55
+
56
+ @Test
57
+ public void jsonSerializationTest () {
58
+ Map<String , Object > map = new HashMap<> ();
59
+ map. put(" name" , " John Doe" );
60
+ map. put(" age" , 40 );
61
+
62
+ expect(map)
63
+ .serializer(" json" )
64
+ .toMatchSnapshot();
65
+ }
47
66
}
48
67
```
49
68
@@ -79,16 +98,17 @@ We currently support:
79
98
- [ JUnit5] ( https://search.maven.org/search?q=a:java-snapshot-testing-junit5 )
80
99
- [ Spock] ( https://search.maven.org/search?q=a:java-snapshot-testing-spock )
81
100
82
- In addition - for ` .json() ` tests, you need jackson on your classpath
83
-
84
- Gradle example
85
- ``` groovy
86
- // Required java-snapshot-testing peer dependencies
87
- testImplementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
88
- testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.11.3'
89
- testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.3'
90
- testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.3'
91
- ```
101
+ Plugins
102
+ - [ Jackson for JSON serialization] ( https://search.maven.org/search?q=a:java-snapshot-testing-plugin-jackson )
103
+ - You need jackson on your classpath
104
+ Gradle example
105
+ ``` groovy
106
+ // Required java-snapshot-testing peer dependencies
107
+ testImplementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
108
+ testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.11.3'
109
+ testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.11.3'
110
+ testImplementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.11.3'
111
+ ```
92
112
93
113
## How does it work?
94
114
1. When a test runs for the first time, a `.snap` file is created in a `__snapshots__` sub-directory
@@ -104,8 +124,7 @@ A text representation of your java object (toString() or JSON).
104
124
105
125
**String snapshot example**
106
126
```java
107
- // `.string()` is the default so not strictly required (unless you override the default!)
108
- expect(" hello world" , " Hello world again!" ). string(). toMatchSnapshot();
127
+ expect("hello world", "Hello world again!").toMatchSnapshot();
109
128
```
110
129
``` text
111
130
au.com.example.company.HelloWorldTest.helloWorld=[
@@ -116,7 +135,7 @@ Hello world again!
116
135
117
136
** JSON Snapshot Example**
118
137
``` java
119
- expect(userDto). json( ). toMatchSnapshot();
138
+ expect(userDto). serializer( " json " ). toMatchSnapshot();
120
139
```
121
140
``` text
122
141
au.com.example.company.UserEndpointTest.shouldReturnCustomerData=[
@@ -229,18 +248,20 @@ Often your IDE has an excellent file comparison tool.
229
248
## snapshot. properties (required as of v2. 4.0 )
230
249
This file allows you to conveniently setup global defaults
231
250
232
- | key | Description |
233
- | -------------- | -------------------------------------------------------------------------------------------------- |
234
- | serializer | Class name of the [serializer](#supplying- a- custom- snapshotserializer) |
235
- | comparator | Class name of the [comparator](#supplying- a- custom- snapshotcomparator) |
236
- | reporters | Comma separated list of class names to use as [reporters](#supplying-a-custom-snapshotreporter) |
237
- |snapshot-dir | Name of sub-folder holding your snapshots |
238
- |output-dir | Base directory of your test files (although it can be a different directory if you want) |
239
- |ci-env-var | Name of environment variable used to detect if we are running on a Build Server |
251
+ | key | Description |
252
+ | ------------------ | ---------------------------------------------------------------------------------------------------------------- |
253
+ | serializer | Class name of the [serializer](#supplying- a- custom- snapshotserializer), default serializer |
254
+ | serializer. {name} | Class name of the [serializer](#supplying- a- custom- snapshotserializer), accessible via `. serializer(" {name}" )` |
255
+ | comparator | Class name of the [comparator](#supplying- a- custom- snapshotcomparator) |
256
+ | reporters | Comma separated list of class names to use as [reporters](#supplying-a-custom-snapshotreporter) |
257
+ |snapshot-dir | Name of sub-folder holding your snapshots |
258
+ |output-dir | Base directory of your test files (although it can be a different directory if you want) |
259
+ |ci-env-var | Name of environment variable used to detect if we are running on a Build Server |
240
260
241
261
For example:
242
262
```text
243
263
serializer=au.com.origin.snapshots.serializers.ToStringSnapshotSerializer
264
+ serializer.json=au.com.origin.snapshots.serializers.DeterministicJacksonSnapshotSerializer
244
265
comparator=au.com.origin.snapshots.comparators.PlainTextEqualsComparator
245
266
reporters=au.com.origin.snapshots.reporters.PlainTextSnapshotReporter
246
267
snapshot-dir=__snapshots__
@@ -279,30 +300,34 @@ The serializer determines how a class gets converted into a string.
279
300
280
301
Currently, we support three different serializers
281
302
282
- | Serializer | Alias | Description |
283
- | ----------------------------------------| ----------------| -----------------------------------------------------------------------------------------------------------------------------|
284
- | ToStringSnapshotSerializer (default) | .string() | uses the ` toString() ` method |
285
- | JacksonSnapshotSerializer | .json() | uses [ jackson] ( https://github.com/FasterXML/jackson ) to convert a class to a snapshot |
286
- | DeterministicJacksonSnapshotSerializer | .orderedJson() | extension of JacksonSnapshotSerializer that also orders Collections for situations where the order changes on multiple runs |
287
- | Base64SnapshotSerializer | | use for images or other binary sources that output a ` byte[] ` . The output is encoded to Base64 |
303
+ ### Shipped with core
304
+ | Serializer | Description |
305
+ | ----------------------------------------| -----------------------------------------------------------------------------------------------------------------------------|
306
+ | ToStringSnapshotSerializer | uses the ` toString() ` method |
307
+ | Base64SnapshotSerializer | use for images or other binary sources that output a ` byte[] ` . The output is encoded to Base64 |
308
+
309
+ ### Shipped with Jackson plugin
310
+ | Serializer | Description |
311
+ | ----------------------------------------| -----------------------------------------------------------------------------------------------------------------------------|
312
+ | JacksonSnapshotSerializer | uses [ jackson] ( https://github.com/FasterXML/jackson ) to convert a class to a snapshot |
313
+ | DeterministicJacksonSnapshotSerializer | extension of JacksonSnapshotSerializer that also orders Collections for situations where the order changes on multiple runs |
288
314
289
315
Serializers are pluggable, so you can write you own by implementing the ` SnapshotSerializer ` interface.
290
316
291
317
Serializers are resolved in the following order.
292
- - (method level) explicitly ` expect(...).serializer(ToStringSerializer.class).toMatchSnapshot(); `
318
+ - (method level) explicitly ` expect(...).serializer(ToStringSerializer.class).toMatchSnapshot(); ` or via property file ` expect(...).serializer("json").toMatchSnapshot(); `
293
319
- (class level) explicitly ` @UseSnapshotConfig ` which gets read from the ` getSerializer() ` method
294
- - (properties) explicitly via snapshot.properties
295
- - (global) implicitly via ` SnapshotConfig ` default for your test framework
320
+ - (properties) implicitly via ` snapshot.properties `
296
321
297
322
``` java
298
323
@ExtendWith (SnapshotExtension . class)
299
324
@UseSnapshotConfig (LowercaseToStringSnapshotConfig . class)
300
325
public class SnapshotExtensionUsedTest {
301
-
326
+
302
327
@Test
303
328
public void aliasMethodTest () {
304
329
expect(new TestObject ())
305
- .orderedJson( ) // <------ Using alias() method
330
+ .serializer( " json " ) // <------ Using snapshot.properties
306
331
.toMatchSnapshot();
307
332
}
308
333
@@ -388,7 +413,6 @@ Comparators follow the same resolution order as Serializers
388
413
1 . method
389
414
1 . class
390
415
1 . snapshot.properties
391
- 1 . global
392
416
393
417
### Example: JsonObjectComparator
394
418
The default comparator may be too strict for certain types of data.
0 commit comments