|
| 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 | +} |
0 commit comments