Skip to content

Commit d8a1d66

Browse files
committed
Fix tests logic to get method from stack and backward compatibility
1 parent 4de7788 commit d8a1d66

File tree

8 files changed

+191
-6
lines changed

8 files changed

+191
-6
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Based on [facebook's Jest framework](https://facebook.github.io/jest/docs/en/sna
1818

1919

2020

21-
#### How to install using [Maven](https://mvnrepository.com/artifact/io.github.json-snapshot/json-snapshot/1.0.16)
21+
#### How to install using [Maven](https://mvnrepository.com/artifact/io.github.json-snapshot/json-snapshot/1.0.17)
2222

2323

2424

@@ -28,7 +28,7 @@ Add to your pom.xml dependencies section:
2828
<dependency>
2929
<groupId>io.github.json-snapshot</groupId>
3030
<artifactId>json-snapshot</artifactId>
31-
<version>1.0.16</version>
31+
<version>1.0.17</version>
3232
</dependency>
3333
```
3434

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>io.github.json-snapshot</groupId>
77
<artifactId>json-snapshot</artifactId>
8-
<version>1.0.16</version>
8+
<version>1.0.17</version>
99
<packaging>jar</packaging>
1010

1111
<name>json-snapshot</name>

src/main/java/io/github/jsonSnapshot/SnapshotMatcher.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ private static void validateExpectCall(Snapshot snapshot) {
162162

163163
private static Method getMethod(Class<?> clazz, String methodName) {
164164
try {
165-
return clazz.getDeclaredMethod(methodName);
165+
return Stream.of(clazz.getDeclaredMethods())
166+
.filter(method -> method.getName().equals(methodName))
167+
.findFirst()
168+
.orElseThrow(() -> new NoSuchMethodException("Not Found"));
166169
} catch (NoSuchMethodException e) {
167170
return Optional.ofNullable(clazz.getSuperclass())
168171
.map(superclass -> getMethod(superclass, methodName))

src/main/java/io/github/jsonSnapshot/SnapshotUtils.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ private static <T> HashMap<String, List<LinkedHashMap<String, Object>>> process(
4747
SnapshotCaptor[] snapshotCaptors) {
4848
HashMap<String, List<LinkedHashMap<String, Object>>> result = new HashMap<>();
4949
try {
50-
Parameter[] parameters = object.getClass().getMethod(methodName, classes).getParameters();
50+
Parameter[] parameters =
51+
object.getClass().getDeclaredMethod(methodName, classes).getParameters();
5152

5253
object
5354
.getClass()
54-
.getMethod(methodName, classes)
55+
.getDeclaredMethod(methodName, classes)
5556
.invoke(
5657
verify(object, atLeastOnce()),
5758
captors.stream().map(ArgumentCaptor::capture).toArray());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package io.github.jsonSnapshot;
2+
3+
import static io.github.jsonSnapshot.SnapshotMatcher.*;
4+
import static io.github.jsonSnapshot.SnapshotUtils.extractArgs;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import org.junit.AfterClass;
10+
import org.junit.BeforeClass;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.mockito.Mock;
14+
import org.mockito.Mockito;
15+
import org.mockito.runners.MockitoJUnitRunner;
16+
17+
@RunWith(MockitoJUnitRunner.class)
18+
public class BackwardCompatilbleTest {
19+
20+
@Mock private FakeObject fakeObject;
21+
22+
@BeforeClass
23+
public static void beforeAll() {
24+
start();
25+
}
26+
27+
@AfterClass
28+
public static void afterAll() {
29+
validateSnapshots();
30+
}
31+
32+
@Test // Snapshot any object
33+
public void shouldShowSnapshotExample() {
34+
expect("<any type of object>").toMatchSnapshot();
35+
}
36+
37+
@Test // Snapshot arguments passed to mocked object (from Mockito library)
38+
public void shouldExtractArgsFromMethod() {
39+
fakeObject.fakeMethod("test1", 1L, Arrays.asList("listTest1"));
40+
fakeObject.fakeMethod("test2", 2L, Arrays.asList("listTest1", "listTest2"));
41+
42+
expect(
43+
extractArgs(
44+
fakeObject,
45+
"fakeMethod",
46+
new SnapshotCaptor(String.class),
47+
new SnapshotCaptor(Long.class),
48+
new SnapshotCaptor(List.class)))
49+
.toMatchSnapshot();
50+
}
51+
52+
@Test // Snapshot arguments passed to mocked object support ignore of fields
53+
public void shouldExtractArgsFromFakeMethodWithComplexObject() {
54+
FakeObject fake = new FakeObject();
55+
fake.setId("idMock");
56+
fake.setName("nameMock");
57+
58+
// With Ignore
59+
fakeObject.fakeMethodWithComplexObject(fake);
60+
Object fakeMethodWithComplexObjectWithIgnore =
61+
extractArgs(
62+
fakeObject,
63+
"fakeMethodWithComplexObject",
64+
new SnapshotCaptor(Object.class, FakeObject.class, "name"));
65+
66+
Mockito.reset(fakeObject);
67+
68+
// Without Ignore of fields
69+
fakeObject.fakeMethodWithComplexObject(fake);
70+
Object fakeMethodWithComplexObjectWithoutIgnore =
71+
extractArgs(
72+
fakeObject,
73+
"fakeMethodWithComplexObject",
74+
new SnapshotCaptor(Object.class, FakeObject.class));
75+
76+
expect(fakeMethodWithComplexObjectWithIgnore, fakeMethodWithComplexObjectWithoutIgnore)
77+
.toMatchSnapshot();
78+
}
79+
80+
class FakeObject {
81+
82+
private String id;
83+
84+
private Integer value;
85+
86+
private String name;
87+
88+
void fakeMethod(String fakeName, Long fakeNumber, List<String> fakeList) {}
89+
90+
void fakeMethodWithComplexObject(Object fakeObj) {}
91+
92+
void setId(String id) {
93+
this.id = id;
94+
}
95+
96+
void setName(String name) {
97+
this.name = name;
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
io.github.jsonSnapshot.BackwardCompatilbleTest.shouldExtractArgsFromFakeMethodWithComplexObject=[
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.BackwardCompatilbleTest.shouldExtractArgsFromMethod=[
25+
{
26+
"FakeObject.fakeMethod": [
27+
{
28+
"arg0": "test1",
29+
"arg1": 1,
30+
"arg2": [
31+
"listTest1"
32+
]
33+
},
34+
{
35+
"arg0": "test2",
36+
"arg1": 2,
37+
"arg2": [
38+
"listTest1",
39+
"listTest2"
40+
]
41+
}
42+
]
43+
}
44+
]
45+
46+
47+
io.github.jsonSnapshot.BackwardCompatilbleTest.shouldShowSnapshotExample=[
48+
"<any type of object>"
49+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.github.jsonSnapshot;
2+
3+
import static io.github.jsonSnapshot.SnapshotMatcher.expect;
4+
5+
import org.junit.jupiter.api.AfterAll;
6+
import org.junit.jupiter.api.BeforeAll;
7+
import org.junit.jupiter.api.Test;
8+
9+
class PrivateCalledMethodTest {
10+
11+
@BeforeAll
12+
static void beforeAll() {
13+
SnapshotMatcher.start();
14+
}
15+
16+
@AfterAll
17+
static void afterAll() {
18+
SnapshotMatcher.validateSnapshots();
19+
}
20+
21+
@Test
22+
void testName() {
23+
testBasedOnArgs("testContent");
24+
}
25+
26+
private void testBasedOnArgs(String arg) {
27+
expect(arg).toMatchSnapshot();
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
io.github.jsonSnapshot.PrivateCalledMethodTest.testName=[
2+
"testContent"
3+
]

0 commit comments

Comments
 (0)