Skip to content

Commit ab9dd4f

Browse files
committed
Change inheritance issues while testing
1 parent ba00ddc commit ab9dd4f

File tree

6 files changed

+101
-3
lines changed

6 files changed

+101
-3
lines changed

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.3</version>
31+
<version>1.0.4</version>
3232
</dependency>
3333
```
3434

@@ -129,4 +129,58 @@ com.example.ExampleTest.shouldExtractArgsFromMethod=[
129129
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.
130130

131131

132+
#### Inheritance
132133

134+
Test classes inheritance becames usefull with snapshot testing due to the fact that the assertions are variable following snasphots, instead of code.
135+
To make usage of this benefit you should be aware of the following:
136+
137+
Start SnapshotMatcher on child classes only:
138+
139+
```java
140+
package com.example;
141+
142+
import org.junit.AfterClass;
143+
import org.junit.BeforeClass;
144+
145+
import static io.github.jsonSnapshot.SnapshotMatcher.start;
146+
import static io.github.jsonSnapshot.SnapshotMatcher.validateSnapshots;
147+
148+
public class SnapshotChildClassTest extends SnapshotSuperClassTest {
149+
150+
@BeforeClass
151+
public static void beforeAll() {
152+
start();
153+
}
154+
155+
@AfterClass
156+
public static void afterAll() {
157+
validateSnapshots();
158+
}
159+
160+
@Override
161+
public String getName() {
162+
return "anyName";
163+
}
164+
}
165+
```
166+
167+
Super classes can have @Test defined, but you should make the class abstract.
168+
169+
```java
170+
package com.example;
171+
172+
import org.junit.Test;
173+
174+
import static io.github.jsonSnapshot.SnapshotMatcher.expect;
175+
176+
public abstract class SnapshotSuperClassTest {
177+
178+
public abstract String getName();
179+
180+
@Test
181+
public void shouldMatchSnapshotOne() {
182+
expect(getName()).toMatchSnapshot();
183+
}
184+
185+
}
186+
```

pom.xml

Lines changed: 1 addition & 1 deletion
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.3</version>
8+
<version>1.0.4</version>
99
<packaging>jar</packaging>
1010

1111
<name>json-snapshot</name>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static void validateExpectCall(Snapshot snapshot) {
8888
private static Method getMethod(StackTraceElement testClass, Class clazz) {
8989
Method method;
9090
try {
91-
method = clazz.getDeclaredMethod(testClass.getMethodName());
91+
method = clazz.getMethod(testClass.getMethodName());
9292
} catch (NoSuchMethodException e) {
9393
throw new SnapshotMatchException("Please annotate your test method with @Test and make it without any parameters!");
9494
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.github.jsonSnapshot;
2+
3+
import org.junit.AfterClass;
4+
import org.junit.BeforeClass;
5+
6+
import static io.github.jsonSnapshot.SnapshotMatcher.start;
7+
import static io.github.jsonSnapshot.SnapshotMatcher.validateSnapshots;
8+
9+
public class SnapshotOverrideClassTest extends SnapshotSuperClassTest {
10+
11+
@BeforeClass
12+
public static void beforeAll() {
13+
start();
14+
}
15+
16+
@AfterClass
17+
public static void afterAll() {
18+
validateSnapshots();
19+
}
20+
21+
@Override
22+
public String getName() {
23+
return "anyName";
24+
}
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
io.github.jsonSnapshot.SnapshotOverrideClassTest.shouldMatchSnapshotOne=[
2+
"anyName"
3+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.github.jsonSnapshot;
2+
3+
import org.junit.Test;
4+
5+
import static io.github.jsonSnapshot.SnapshotMatcher.expect;
6+
7+
public abstract class SnapshotSuperClassTest {
8+
9+
public abstract String getName();
10+
11+
@Test
12+
public void shouldMatchSnapshotOne() {
13+
expect(getName()).toMatchSnapshot();
14+
}
15+
16+
}

0 commit comments

Comments
 (0)