Skip to content

Commit 580361c

Browse files
committed
Compatibility changes to make lib work with Java 11 and Junit 5
1 parent 01755b3 commit 580361c

10 files changed

+97
-93
lines changed

.mvn/jvm.config

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--add-opens java.base/java.lang=ALL-UNNAMED

pom.xml

+35-13
Original file line numberDiff line numberDiff line change
@@ -53,38 +53,58 @@
5353
<dependency>
5454
<groupId>com.google.code.gson</groupId>
5555
<artifactId>gson</artifactId>
56-
<version>2.8.2</version>
56+
<version>2.8.5</version>
5757
</dependency>
5858
<dependency>
5959
<groupId>org.projectlombok</groupId>
6060
<artifactId>lombok</artifactId>
61-
<version>1.16.18</version>
61+
<version>1.18.4</version>
6262
<scope>provided</scope>
6363
</dependency>
6464
<dependency>
6565
<groupId>org.apache.commons</groupId>
6666
<artifactId>commons-lang3</artifactId>
67-
<version>3.7</version>
67+
<version>3.8.1</version>
6868
</dependency>
6969
<dependency>
70-
<groupId>junit</groupId>
71-
<artifactId>junit</artifactId>
72-
<version>4.12</version>
70+
<groupId>org.junit.jupiter</groupId>
71+
<artifactId>junit-jupiter-api</artifactId>
72+
<version>5.3.2</version>
73+
</dependency>
74+
<dependency>
75+
<groupId>org.junit.jupiter</groupId>
76+
<artifactId>junit-jupiter-engine</artifactId>
77+
<version>5.3.2</version>
78+
</dependency>
79+
<dependency>
80+
<groupId>org.mockito</groupId>
81+
<artifactId>mockito-junit-jupiter</artifactId>
82+
<version>2.23.0</version>
7383
</dependency>
7484
<dependency>
7585
<groupId>org.assertj</groupId>
7686
<artifactId>assertj-core</artifactId>
77-
<version>3.8.0</version>
87+
<version>3.11.1</version>
7888
</dependency>
7989
<dependency>
8090
<groupId>org.mockito</groupId>
81-
<artifactId>mockito-all</artifactId>
82-
<version>1.10.19</version>
91+
<artifactId>mockito-core</artifactId>
92+
<version>2.23.4</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.junit.platform</groupId>
96+
<artifactId>junit-platform-runner</artifactId>
97+
<version>1.2.0</version>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.junit.vintage</groupId>
101+
<artifactId>junit-vintage-engine</artifactId>
102+
<version>5.2.0</version>
83103
</dependency>
84104
<dependency>
85105
<groupId>org.slf4j</groupId>
86106
<artifactId>slf4j-api</artifactId>
87-
<version>1.7.25</version>
107+
<version>1.8.0-beta2</version>
88108
</dependency>
89109
</dependencies>
90110

@@ -93,10 +113,9 @@
93113
<plugin>
94114
<groupId>org.apache.maven.plugins</groupId>
95115
<artifactId>maven-compiler-plugin</artifactId>
96-
<version>3.7.0</version>
116+
<version>3.8.0</version>
97117
<configuration>
98-
<source>1.8</source>
99-
<target>1.8</target>
118+
<release>11</release>
100119
</configuration>
101120
</plugin>
102121
<plugin>
@@ -116,6 +135,9 @@
116135
<groupId>org.apache.maven.plugins</groupId>
117136
<artifactId>maven-javadoc-plugin</artifactId>
118137
<version>3.0.0</version>
138+
<configuration>
139+
<javadocExecutable>${java.home}/bin/javadoc</javadocExecutable>
140+
</configuration>
119141
<executions>
120142
<execution>
121143
<id>attach-javadocs</id>

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ private Object shallowCopy(Object value) {
7272
}
7373

7474
private Object constructCopy(Class<?> argumentClass)
75-
throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException {
75+
throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException {
7676

7777
try {
78-
return argumentClass.newInstance();
78+
return argumentClass.getDeclaredConstructor().newInstance();
7979
}
8080
catch (Exception e) {
8181
// Ignore - should log
@@ -84,7 +84,7 @@ private Object constructCopy(Class<?> argumentClass)
8484
Constructor[] constructors = argumentClass.getDeclaredConstructors();
8585

8686
if (constructors.length == 0) {
87-
return argumentClass.newInstance();
87+
return argumentClass.getDeclaredConstructor().newInstance();
8888
}
8989

9090
int i = 0;

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.assertj.core.util.Arrays;
77
import org.junit.BeforeClass;
88
import org.junit.Test;
9+
import org.junit.jupiter.api.BeforeAll;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -15,7 +16,6 @@
1516
import java.util.List;
1617
import java.util.Set;
1718
import java.util.function.Function;
18-
import java.util.function.Supplier;
1919
import java.util.stream.Collectors;
2020

2121
public class SnapshotMatcher {
@@ -127,7 +127,8 @@ private static StackTraceElement findStackElement() throws ClassNotFoundExceptio
127127
Method method = clazz.getMethod(stackTraceElements[j].getMethodName());
128128

129129
// Navigate into stack until Test class/method level
130-
if (method.isAnnotationPresent(Test.class) || method.isAnnotationPresent(BeforeClass.class)) {
130+
if (method.isAnnotationPresent(Test.class) || method.isAnnotationPresent(BeforeClass.class) ||
131+
method.isAnnotationPresent(org.junit.jupiter.api.Test.class) || method.isAnnotationPresent(BeforeAll.class)) {
131132
i = j;
132133
break;
133134
}

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

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
package io.github.jsonSnapshot;
22

3-
import org.hamcrest.core.StringStartsWith;
4-
import org.junit.AfterClass;
5-
import org.junit.BeforeClass;
6-
import org.junit.Rule;
7-
import org.junit.Test;
8-
import org.junit.rules.ExpectedException;
9-
import org.junit.runner.RunWith;
10-
import org.mockito.runners.MockitoJUnitRunner;
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.BeforeAll;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.mockito.junit.jupiter.MockitoExtension;
118

129
import static io.github.jsonSnapshot.SnapshotMatcher.*;
10+
import static org.junit.jupiter.api.Assertions.assertThrows;
1311

14-
@RunWith(MockitoJUnitRunner.class)
12+
@ExtendWith(MockitoExtension.class)
1513
public class SnapshotIntegrationTest {
1614

17-
@Rule
18-
public ExpectedException expectedException = ExpectedException.none();
19-
20-
@BeforeClass
15+
@BeforeAll
2116
public static void beforeAll() {
2217
start();
2318
}
2419

25-
@AfterClass
20+
@AfterAll
2621
public static void afterAll() {
2722
validateSnapshots();
2823
}
@@ -58,22 +53,18 @@ private void matchInsidePrivate() {
5853

5954
@Test
6055
public void shouldThrowSnapshotMatchException() {
61-
expectedException.expect(SnapshotMatchException.class);
62-
expectedException.expectMessage(StringStartsWith.startsWith("Error on: \n" +
63-
"io.github.jsonSnapshot.SnapshotIntegrationTest.shouldThrowSnapshotMatchException=["));
64-
expect(FakeObject.builder().id("anyId5").value(6).name("anyName5").build()).toMatchSnapshot();
56+
assertThrows(SnapshotMatchException.class, expect(FakeObject.builder().id("anyId5").value(6).name("anyName5").build())::toMatchSnapshot, "Error on: \n" +
57+
"io.github.jsonSnapshot.SnapshotIntegrationTest.shouldThrowSnapshotMatchException=[");
6558
}
6659

6760
@Test
6861
public void shouldThrowStackOverflowError() {
69-
expectedException.expect(StackOverflowError.class);
70-
7162
// Create cycle JSON
7263
FakeObject fakeObject1 = FakeObject.builder().id("anyId1").value(1).name("anyName1").build();
7364
FakeObject fakeObject2 = FakeObject.builder().id("anyId2").value(2).name("anyName2").build();
7465
fakeObject1.setFakeObject(fakeObject2);
7566
fakeObject2.setFakeObject(fakeObject1);
7667

77-
expect(fakeObject1).toMatchSnapshot();
68+
assertThrows(StackOverflowError.class, () -> expect(fakeObject1).toMatchSnapshot());
7869
}
7970
}
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package io.github.jsonSnapshot;
22

33
import org.apache.commons.lang3.StringUtils;
4-
import org.junit.AfterClass;
5-
import org.junit.BeforeClass;
6-
import org.junit.FixMethodOrder;
7-
import org.junit.Test;
8-
import org.junit.runner.RunWith;
9-
import org.junit.runners.MethodSorters;
10-
import org.mockito.runners.MockitoJUnitRunner;
4+
import org.junit.jupiter.api.AfterAll;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.junit.jupiter.MockitoExtension;
119

1210
import java.io.File;
1311
import java.io.IOException;
@@ -16,20 +14,28 @@
1614

1715
import static org.assertj.core.api.Assertions.assertThat;
1816

19-
@RunWith(MockitoJUnitRunner.class)
20-
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
17+
@ExtendWith(MockitoExtension.class)
2118
public class SnapshotMatcherTest {
2219

2320
private static final String FILE_PATH = "src/test/java/io/github/jsonSnapshot/SnapshotMatcherTest.snap";
2421

25-
@BeforeClass
22+
@BeforeAll
2623
public static void beforeAll() {
2724
SnapshotMatcher.start();
2825
}
2926

30-
@AfterClass
27+
@AfterAll
3128
public static void afterAll() throws IOException {
3229
SnapshotMatcher.validateSnapshots();
30+
File f = new File(FILE_PATH);
31+
assertThat(StringUtils.join(Files.readAllLines(f.toPath()), "\n")).
32+
isEqualTo("io.github.jsonSnapshot.SnapshotMatcherTest.should1ShowSnapshotSuccessfully=[\n" +
33+
" \"any type of object\"\n" +
34+
"]\n\n\n" +
35+
"io.github.jsonSnapshot.SnapshotMatcherTest.should2SecondSnapshotExecutionSuccessfully=[\n" +
36+
" \"any second type of object\",\n" +
37+
" \"any third type of object\"\n" +
38+
"]");
3339
Files.delete(Paths.get(FILE_PATH));
3440
}
3541

@@ -41,16 +47,7 @@ public void should1ShowSnapshotSuccessfully() throws IOException {
4147
if(!f.exists() || f.isDirectory()) {
4248
throw new RuntimeException("File should exist here");
4349
}
44-
45-
assertThat(StringUtils.join(Files.readAllLines(f.toPath()), "\n")).isEqualTo("");
46-
4750
SnapshotMatcher.expect("any type of object").toMatchSnapshot();
48-
49-
assertThat(StringUtils.join(Files.readAllLines(f.toPath()), "\n")).
50-
isEqualTo("io.github.jsonSnapshot.SnapshotMatcherTest.should1ShowSnapshotSuccessfully=[\n" +
51-
" \"any type of object\"\n" +
52-
"]");
53-
5451
}
5552

5653
@Test
@@ -60,17 +57,6 @@ public void should2SecondSnapshotExecutionSuccessfully() throws IOException {
6057
if(!f.exists() || f.isDirectory()) {
6158
throw new RuntimeException("File should exist here");
6259
}
63-
6460
SnapshotMatcher.expect("any second type of object", "any third type of object").toMatchSnapshot();
65-
66-
assertThat(StringUtils.join(Files.readAllLines(f.toPath()), "\n")).
67-
isEqualTo("io.github.jsonSnapshot.SnapshotMatcherTest.should1ShowSnapshotSuccessfully=[\n" +
68-
" \"any type of object\"\n" +
69-
"]\n\n\n" +
70-
"io.github.jsonSnapshot.SnapshotMatcherTest.should2SecondSnapshotExecutionSuccessfully=[\n" +
71-
" \"any second type of object\",\n" +
72-
" \"any third type of object\"\n" +
73-
"]");
74-
7561
}
7662
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package io.github.jsonSnapshot;
22

3-
import org.junit.AfterClass;
4-
import org.junit.BeforeClass;
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.BeforeAll;
55

66
import static io.github.jsonSnapshot.SnapshotMatcher.start;
77
import static io.github.jsonSnapshot.SnapshotMatcher.validateSnapshots;
88

99
public class SnapshotOverrideClassTest extends SnapshotSuperClassTest {
1010

11-
@BeforeClass
11+
@BeforeAll
1212
public static void beforeAll() {
1313
start();
1414
}
1515

16-
@AfterClass
16+
@AfterAll
1717
public static void afterAll() {
1818
validateSnapshots();
1919
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.jsonSnapshot;
22

3-
import org.junit.Test;
3+
4+
import org.junit.jupiter.api.Test;
45

56
import static io.github.jsonSnapshot.SnapshotMatcher.expect;
67

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.github.jsonSnapshot;
22

33
import com.google.gson.GsonBuilder;
4-
import org.junit.After;
5-
import org.junit.Before;
6-
import org.junit.Test;
7-
import org.junit.runner.RunWith;
8-
import org.mockito.runners.MockitoJUnitRunner;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.junit.jupiter.api.extension.ExtendWith;
8+
import org.mockito.junit.jupiter.MockitoExtension;
99

1010
import java.io.IOException;
1111
import java.nio.file.Files;
@@ -15,8 +15,9 @@
1515
import java.util.stream.Stream;
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1819

19-
@RunWith(MockitoJUnitRunner.class)
20+
@ExtendWith(MockitoExtension.class)
2021
public class SnapshotTest {
2122

2223
private static final SnapshotConfig DEFAULT_CONFIG = new DefaultConfig();
@@ -29,15 +30,15 @@ public class SnapshotTest {
2930
private Snapshot snapshot;
3031

3132

32-
@Before
33+
@BeforeEach
3334
public void setUp() throws NoSuchMethodException, IOException {
3435
snapshotFile = new SnapshotFile(DEFAULT_CONFIG.getFilePath(), "anyFilePath");
3536
snapshot = new Snapshot(snapshotFile, String.class,
3637
String.class.getDeclaredMethod("toString"),
3738
(object) -> new GsonBuilder().setPrettyPrinting().create().toJson(object),"anyObject");
3839
}
3940

40-
@After
41+
@AfterEach
4142
public void tearDown() throws IOException {
4243
Files.delete(Paths.get(FILE_PATH));
4344
}
@@ -54,10 +55,11 @@ public void shouldMatchSnapshotSuccessfully() {
5455
assertThat(snapshotFile.getRawSnapshots()).isEqualTo(Stream.of(SNAPSHOT).collect(Collectors.toCollection(TreeSet::new)));
5556
}
5657

57-
@Test(expected = SnapshotMatchException.class)
58+
@Test
5859
public void shouldMatchSnapshotWithException() {
5960
snapshotFile.push(SNAPSHOT_NAME + "anyWrongSnapshot");
60-
snapshot.toMatchSnapshot();
61+
62+
assertThrows(SnapshotMatchException.class, snapshot::toMatchSnapshot);
6163
}
6264

6365
}

0 commit comments

Comments
 (0)