Skip to content

Commit eacbd77

Browse files
author
Andre Bonna
committed
2 parents 80c3993 + 0dce29b commit eacbd77

File tree

4 files changed

+157
-13
lines changed

4 files changed

+157
-13
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import java.util.List;
5050

5151
import static io.github.jsonSnapshot.SnapshotMatcher.*;
5252
import static io.github.jsonSnapshot.SnapshotUtils.*;
53+
import io.github.jsonSnapshot.SnapshotCaptor;
5354

5455
@RunWith(MockitoJUnitRunner.class)
5556
public class ExampleTest {
@@ -78,11 +79,35 @@ public class ExampleTest {
7879
fakeObject.fakeMethod("test1", 1L, Arrays.asList("listTest1"));
7980
fakeObject.fakeMethod("test2", 2L, Arrays.asList("listTest1", "listTest2"));
8081

81-
expect(extractArgs(fakeObject, "fakeMethod", String.class, Long.class, List.class))
82+
expect(extractArgs(fakeObject, "fakeMethod", new SnapshotCaptor(String.class), new SnapshotCaptor(Long.class), new SnapshotCaptor(List.class)))
8283
.toMatchSnapshot();
8384
}
8485

86+
@Test // Snapshot arguments passed to mocked object support ignore of fields
87+
public void shouldExtractArgsFromFakeMethodWithComplexObject() {
88+
FakeObject fake = new FakeObject();
89+
fake.setId("idMock");
90+
fake.setName("nameMock");
91+
92+
//With Ignore
93+
fakeObject.fakeMethodWithComplexObject(fake);
94+
Object fakeMethodWithComplexObjectWithIgnore = extractArgs(
95+
fakeObject, "fakeMethodWithComplexObject",
96+
new SnapshotCaptor(Object.class, FakeObject.class, "name"));
97+
98+
Mockito.reset(fakeObject);
99+
100+
// Without Ignore of fields
101+
fakeObject.fakeMethodWithComplexObject(fake);
102+
Object fakeMethodWithComplexObjectWithoutIgnore = extractArgs(
103+
fakeObject, "fakeMethodWithComplexObject",
104+
new SnapshotCaptor(Object.class, FakeObject.class));
105+
106+
expect(fakeMethodWithComplexObjectWithIgnore, fakeMethodWithComplexObjectWithoutIgnore).toMatchSnapshot();
107+
}
108+
85109
class FakeObject {
110+
86111
private String id;
87112

88113
private Integer value;
@@ -92,6 +117,18 @@ public class ExampleTest {
92117
void fakeMethod(String fakeName, Long fakeNumber, List<String> fakeList) {
93118

94119
}
120+
121+
void fakeMethodWithComplexObject(Object fakeObj) {
122+
123+
}
124+
125+
void setId(String id) {
126+
this.id = id;
127+
}
128+
129+
void setName(String name) {
130+
this.name = name;
131+
}
95132
}
96133
}
97134
```
@@ -124,6 +161,29 @@ com.example.ExampleTest.shouldExtractArgsFromMethod=[
124161
]
125162
}
126163
]
164+
165+
166+
com.example.ExampleTest.shouldExtractArgsFromFakeMethodWithComplexObject=[
167+
{
168+
"FakeObject.fakeMethodWithComplexObject": [
169+
{
170+
"arg0": {
171+
"id": "idMock"
172+
}
173+
}
174+
]
175+
},
176+
{
177+
"FakeObject.fakeMethodWithComplexObject": [
178+
{
179+
"arg0": {
180+
"id": "idMock",
181+
"name": "nameMock"
182+
}
183+
}
184+
]
185+
}
186+
]
127187
```
128188

129189
Whenever it runs again, the `expect` method argument will be automatically validated with the `.snap` file. That is why you should commit every `.snap` file created.

src/test/java/io/github/jsonSnapshot/FakeObject.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package io.github.jsonSnapshot;
22

3-
import lombok.Builder;
4-
import lombok.Setter;
3+
import lombok.*;
54

65
import java.util.List;
76

87
@Builder
8+
@NoArgsConstructor
9+
@AllArgsConstructor
910
public class FakeObject {
1011

1112
private String id;
@@ -17,9 +18,15 @@ public class FakeObject {
1718
@Setter
1819
private FakeObject fakeObject;
1920

20-
2121
public void fakeMethod(String fakeName, Long fakeNumber, List<String> fakeList) {
2222

2323
}
2424

25+
public void fakeMethodWithComplexObject(Object fakeObj) {
26+
27+
}
28+
29+
public void fakeMethodWithComplexFakeObject(FakeObject fakeObj) {
30+
31+
}
2532
}

src/test/java/io/github/jsonSnapshot/SnapshotUtilsTest.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
import org.junit.Test;
66
import org.junit.runner.RunWith;
77
import org.mockito.Mock;
8+
import org.mockito.Mockito;
89
import org.mockito.runners.MockitoJUnitRunner;
910

1011
import java.util.Arrays;
11-
import java.util.HashMap;
1212
import java.util.List;
1313

14-
import static io.github.jsonSnapshot.SnapshotMatcher.expect;
15-
import static io.github.jsonSnapshot.SnapshotMatcher.start;
16-
import static io.github.jsonSnapshot.SnapshotMatcher.validateSnapshots;
14+
import static io.github.jsonSnapshot.SnapshotMatcher.*;
15+
import static io.github.jsonSnapshot.SnapshotUtils.extractArgs;
1716

1817
@RunWith(MockitoJUnitRunner.class)
1918
public class SnapshotUtilsTest {
@@ -33,15 +32,47 @@ public static void afterAll() {
3332

3433

3534
@Test
36-
public void shouldExtractArgsFromMethod() {
35+
public void shouldExtractArgsFromFakeMethod() {
3736

3837
fakeObject.fakeMethod("test1", 1L, Arrays.asList("listTest1"));
3938
fakeObject.fakeMethod("test2", 2L, Arrays.asList("listTest1", "listTest2"));
4039

41-
HashMap<?, ?> fakeMethodArgs = SnapshotUtils.extractArgs(fakeObject, "fakeMethod", String.class, Long.class, List.class);
42-
expect(fakeMethodArgs)
43-
.toMatchSnapshot();
40+
Object fakeMethod = extractArgs(fakeObject, "fakeMethod", new SnapshotCaptor(String.class), new SnapshotCaptor(Long.class), new SnapshotCaptor(List.class));
41+
expect(fakeMethod).toMatchSnapshot();
42+
}
43+
44+
@Test
45+
public void shouldExtractArgsFromFakeMethodWithComplexObject() {
46+
FakeObject fake = new FakeObject.FakeObjectBuilder().id("idMock").name("nameMock").build();
47+
48+
//With Ignore
49+
fakeObject.fakeMethodWithComplexFakeObject(fake);
50+
Object fakeMethodWithComplexObjectWithIgnore = extractArgs(fakeObject, "fakeMethodWithComplexFakeObject", new SnapshotCaptor(FakeObject.class, "name"));
4451

52+
Mockito.reset(fakeObject);
53+
54+
// Without Ignore
55+
fakeObject.fakeMethodWithComplexFakeObject(fake);
56+
Object fakeMethodWithComplexObjectWithoutIgnore = extractArgs(fakeObject, "fakeMethodWithComplexFakeObject", new SnapshotCaptor(FakeObject.class));
57+
58+
expect(fakeMethodWithComplexObjectWithIgnore, fakeMethodWithComplexObjectWithoutIgnore).toMatchSnapshot();
4559
}
4660

61+
@Test
62+
public void shouldExtractArgsFromFakeMethodWithComplexFakeObject() {
63+
64+
FakeObject fake = new FakeObject.FakeObjectBuilder().id("idMock").name("nameMock").build();
65+
66+
//With Ignore
67+
fakeObject.fakeMethodWithComplexObject(fake);
68+
Object fakeMethodWithComplexObjectWithIgnore = extractArgs(fakeObject, "fakeMethodWithComplexObject", new SnapshotCaptor(Object.class, FakeObject.class, "name"));
69+
70+
Mockito.reset(fakeObject);
71+
72+
// Without Ignore
73+
fakeObject.fakeMethodWithComplexObject(fake);
74+
Object fakeMethodWithComplexObjectWithoutIgnore = extractArgs(fakeObject, "fakeMethodWithComplexObject", new SnapshotCaptor(Object.class, FakeObject.class));
75+
76+
expect(fakeMethodWithComplexObjectWithIgnore, fakeMethodWithComplexObjectWithoutIgnore).toMatchSnapshot();
77+
}
4778
}

src/test/java/io/github/jsonSnapshot/SnapshotUtilsTest.snap

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,50 @@
1-
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromMethod=[
1+
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethodWithComplexFakeObject=[
2+
{
3+
"FakeObject.fakeMethodWithComplexObject": [
4+
{
5+
"arg0": {
6+
"id": "idMock"
7+
}
8+
}
9+
]
10+
},
11+
{
12+
"FakeObject.fakeMethodWithComplexObject": [
13+
{
14+
"arg0": {
15+
"id": "idMock",
16+
"name": "nameMock"
17+
}
18+
}
19+
]
20+
}
21+
]
22+
23+
24+
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethodWithComplexObject=[
25+
{
26+
"FakeObject.fakeMethodWithComplexFakeObject": [
27+
{
28+
"arg0": {
29+
"id": "idMock"
30+
}
31+
}
32+
]
33+
},
34+
{
35+
"FakeObject.fakeMethodWithComplexFakeObject": [
36+
{
37+
"arg0": {
38+
"id": "idMock",
39+
"name": "nameMock"
40+
}
41+
}
42+
]
43+
}
44+
]
45+
46+
47+
io.github.jsonSnapshot.SnapshotUtilsTest.shouldExtractArgsFromFakeMethod=[
248
{
349
"FakeObject.fakeMethod": [
450
{

0 commit comments

Comments
 (0)