Skip to content

Commit fbcfda3

Browse files
author
Andre Bonna
committed
Fix stackTrace navigation to find test method name
1 parent ab9dd4f commit fbcfda3

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

pom.xml

Lines changed: 6 additions & 11 deletions
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.4</version>
8+
<version>1.0.5</version>
99
<packaging>jar</packaging>
1010

1111
<name>json-snapshot</name>
@@ -66,6 +66,11 @@
6666
<artifactId>commons-lang3</artifactId>
6767
<version>3.7</version>
6868
</dependency>
69+
<dependency>
70+
<groupId>junit</groupId>
71+
<artifactId>junit</artifactId>
72+
<version>4.12</version>
73+
</dependency>
6974
<dependency>
7075
<groupId>org.assertj</groupId>
7176
<artifactId>assertj-core</artifactId>
@@ -81,16 +86,6 @@
8186
<artifactId>slf4j-api</artifactId>
8287
<version>1.7.25</version>
8388
</dependency>
84-
85-
86-
<!-- Test -->
87-
<dependency>
88-
<groupId>junit</groupId>
89-
<artifactId>junit</artifactId>
90-
<version>4.12</version>
91-
<scope>test</scope>
92-
</dependency>
93-
9489
</dependencies>
9590

9691
<build>

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.apache.commons.lang3.ArrayUtils;
44
import org.apache.commons.lang3.StringUtils;
55
import org.assertj.core.util.Arrays;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
68
import org.mockito.ArgumentCaptor;
79
import org.mockito.verification.VerificationMode;
810
import org.slf4j.Logger;
@@ -100,10 +102,27 @@ private static StackTraceElement findStackElement() throws ClassNotFoundExceptio
100102

101103
int i = 1; // Start after stackTrace
102104
while (i < stackTraceElements.length &&
103-
SnapshotMatcher.class.equals(Class.forName(stackTraceElements[i].getClassName()))) {
105+
SnapshotMatcher.class.equals(Class.forName(stackTraceElements[i].getClassName()))) { //TODO
104106
i++;
105107
}
106108

109+
for (int j = i; j < stackTraceElements.length; j++) {
110+
111+
try {
112+
Class clazz = Class.forName(stackTraceElements[j].getClassName());
113+
Method method = clazz.getMethod(stackTraceElements[j].getMethodName());
114+
115+
// Navigate into stack until Test class/method level
116+
if (method.isAnnotationPresent(Test.class) || method.isAnnotationPresent(BeforeClass.class)) {
117+
i = j;
118+
break;
119+
}
120+
}
121+
catch(NoSuchMethodException ignored) {
122+
123+
}
124+
}
125+
107126
return stackTraceElements[i];
108127
}
109128

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public void shouldMatchSnapshotFour() {
4747
expect(FakeObject.builder().id("anyId4").value(4).name("any\n\n\nName4").build()).toMatchSnapshot();
4848
}
4949

50+
@Test
51+
public void shouldMatchSnapshotInsidePrivateMethod() {
52+
matchInsidePrivate();
53+
}
54+
55+
private void matchInsidePrivate() {
56+
expect(FakeObject.builder().id("anyPrivate").value(5).name("anyPrivate").build()).toMatchSnapshot();
57+
}
58+
5059
@Test
5160
public void shouldThrowSnapshotMatchException() {
5261
expectedException.expect(SnapshotMatchException.class);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@ io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotTwo=[
4040
"value": 2,
4141
"name": "anyName2"
4242
}
43+
]
44+
45+
46+
47+
io.github.jsonSnapshot.SnapshotIntegrationTest.shouldMatchSnapshotInsidePrivateMethod=[
48+
{
49+
"id": "anyPrivate",
50+
"value": 5,
51+
"name": "anyPrivate"
52+
}
4353
]

0 commit comments

Comments
 (0)