All Projects → json-snapshot → json-snapshot.github.io

json-snapshot / json-snapshot.github.io

Licence: MIT License
Snapshot Testing for Java

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to json-snapshot.github.io

bUnit
bUnit is a testing library for Blazor components that make tests look, feel, and runs like regular unit tests. bUnit makes it easy to render and control a component under test’s life-cycle, pass parameter and inject services into it, trigger event handlers, and verify the rendered markup from the component using a built-in semantic HTML comparer.
Stars: ✭ 857 (+2960.71%)
Mutual labels:  unit-testing, snapshot-testing
Enzyme To Json
Snapshot test your Enzyme wrappers
Stars: ✭ 954 (+3307.14%)
Mutual labels:  unit-testing, snapshot-testing
Bunit
A testing library for Blazor Components. You can easily define components under test in C# or Razor syntax and verify outcome using semantic HTML diffing/comparison logic. You can easily interact with and inspect components, trigger event handlers, provide cascading values, inject services, mock IJSRuntime, and perform snapshot testing.
Stars: ✭ 415 (+1382.14%)
Mutual labels:  unit-testing, snapshot-testing
meeting-app-unit-tests-playground
Unit tests patterns when using JEST and Vue2 (vue-test-utils)
Stars: ✭ 21 (-25%)
Mutual labels:  unit-testing, snapshot-testing
riteway-jest
Unit tests that always supply a good bug report when they fail for Jest.
Stars: ✭ 24 (-14.29%)
Mutual labels:  unit-testing
tdd roman csharp
Kata: TDD Arabic to Roman Numerals with C#
Stars: ✭ 14 (-50%)
Mutual labels:  unit-testing
SqlInMemory
SqlInMemory is a library for creating SqlServer database on Memory instead of hard disk, at last Drops and Disposes database when you're done with it. This is useful for Integration Testing.
Stars: ✭ 24 (-14.29%)
Mutual labels:  unit-testing
archunit-examples
ArchUnit examples for a ports-and-adapters application architecture inside a Spring Boot book-catalog application
Stars: ✭ 31 (+10.71%)
Mutual labels:  unit-testing
teuton
Infrastructure test, mainly useful for sysadmin teachers and making contests
Stars: ✭ 22 (-21.43%)
Mutual labels:  unit-testing
angular-unit-testing-examples
Showroom for different Angular unit testing concepts
Stars: ✭ 19 (-32.14%)
Mutual labels:  unit-testing
InstantMock
Create mocks easily in Swift
Stars: ✭ 88 (+214.29%)
Mutual labels:  unit-testing
UnitTestBoilerplateGenerator
An extension for Visual Studio that generates a unit test boilerplate from a given class, setting up mocks for all dependencies. Supports NUnit, Visual Studio Test, XUnit and many mock frameworks.
Stars: ✭ 39 (+39.29%)
Mutual labels:  unit-testing
StoreCleanArchitecture-NET
This is a basic project to demonstrate an introduction about the implementation of Clean Architecture on .NET
Stars: ✭ 19 (-32.14%)
Mutual labels:  unit-testing
Battleship
An Object-Oriented VBA experiment
Stars: ✭ 66 (+135.71%)
Mutual labels:  unit-testing
pytest-snapshot
A plugin for snapshot testing with pytest.
Stars: ✭ 68 (+142.86%)
Mutual labels:  snapshot-testing
markbot
An application that automatically tests and marks student code assignments in Algonquin College Graphic Design’s Web Dev courses.
Stars: ✭ 23 (-17.86%)
Mutual labels:  unit-testing
Fineract-CN-mobile
DEPRECATED project - Check the Apache fineract-cn-mobile project instead
Stars: ✭ 17 (-39.29%)
Mutual labels:  unit-testing
angular-material-boilerplate
A straightforward and well structured boilerplate based on Google's Angular Material project.
Stars: ✭ 28 (+0%)
Mutual labels:  unit-testing
Wasmite
Now WebAssembly has proper testing, unit-testing and debugging 🤗
Stars: ✭ 20 (-28.57%)
Mutual labels:  unit-testing
wp-phpunit
WordPress core PHPUnit library. [READ ONLY] Versions for new WordPress releases are built daily.
Stars: ✭ 65 (+132.14%)
Mutual labels:  unit-testing

Purpose of Snapshot Testing

Snapshots help figuring out whether the output of the modules covered by tests is changed, without doing tons of asserts!

When is it usefull?

It's usefull for deterministic tests. That is, running the same tests multiple times on a component that has not changed should produce the same results every time. You're responsible for making sure your generated snapshots do not include platform specific or other non-deterministic data.

A Json Snapshot test does not assert Java types. You can continue doing that with any other testing framework.

Based on facebook's Jest framework

GitHub Repository

How to install using Maven

Add to your pom.xml dependencies section:

<dependency>
    <groupId>io.github.json-snapshot</groupId>
    <artifactId>json-snapshot</artifactId>
    <version>1.0.17</version>
</dependency>

Usage

package com.example;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.List;

import static io.github.jsonSnapshot.SnapshotMatcher.*;
import static io.github.jsonSnapshot.SnapshotUtils.*;
import io.github.jsonSnapshot.SnapshotCaptor;

@RunWith(MockitoJUnitRunner.class)
public class ExampleTest {

    @Mock
    private FakeObject fakeObject;


    @BeforeClass
    public static void beforeAll() {
        start();
    }
    
    @AfterClass
    public static void afterAll() {
        validateSnapshots();
    }
    
    @Test // Snapshot any object
    public void shouldShowSnapshotExample() {
        expect("<any type of object>").toMatchSnapshot();
    }

    @Test // Snapshot arguments passed to mocked object (from Mockito library)
    public void shouldExtractArgsFromMethod() {
        fakeObject.fakeMethod("test1", 1L, Arrays.asList("listTest1"));
        fakeObject.fakeMethod("test2", 2L, Arrays.asList("listTest1", "listTest2"));

        expect(extractArgs(fakeObject, "fakeMethod", new SnapshotCaptor(String.class), new SnapshotCaptor(Long.class), new SnapshotCaptor(List.class)))
                .toMatchSnapshot();
    }
    
    @Test // Snapshot arguments passed to mocked object support ignore of fields
    public void shouldExtractArgsFromFakeMethodWithComplexObject() {
        FakeObject fake = new FakeObject();
        fake.setId("idMock");
        fake.setName("nameMock");

        //With Ignore
        fakeObject.fakeMethodWithComplexObject(fake);
        Object fakeMethodWithComplexObjectWithIgnore = extractArgs(
                fakeObject, "fakeMethodWithComplexObject", 
                new SnapshotCaptor(Object.class, FakeObject.class, "name"));

        Mockito.reset(fakeObject);

        // Without Ignore of fields
        fakeObject.fakeMethodWithComplexObject(fake);
        Object fakeMethodWithComplexObjectWithoutIgnore = extractArgs(
                fakeObject, "fakeMethodWithComplexObject", 
                new SnapshotCaptor(Object.class, FakeObject.class));

        expect(fakeMethodWithComplexObjectWithIgnore, fakeMethodWithComplexObjectWithoutIgnore).toMatchSnapshot();
    }
    
    class FakeObject {
        
        private String id;

        private Integer value;

        private String name;

        void fakeMethod(String fakeName, Long fakeNumber, List<String> fakeList) {

        }
        
        void fakeMethodWithComplexObject(Object fakeObj) {
        
        }
        
        void setId(String id) {
            this.id = id;
        }
        
        void setName(String name) {
            this.name = name;
        }
    }
}

When the test runs for the first time, the framework will create a snapshot file named ExampleTest.snap alongside with your test class. It should look like this:

com.example.ExampleTest.shouldShowSnapshotExample=[
    "<any type of object>"
]


com.example.ExampleTest.shouldExtractArgsFromMethod=[
  {
    "FakeObject.fakeMethod": [
      {
        "arg0": "test1",
        "arg1": 1,
        "arg2": [
          "listTest1"
        ]
      },
      {
        "arg0": "test2",
        "arg1": 2,
        "arg2": [
          "listTest1",
          "listTest2"
        ]
      }
    ]
  }
]


com.example.ExampleTest.shouldExtractArgsFromFakeMethodWithComplexObject=[
  {
    "FakeObject.fakeMethodWithComplexObject": [
      {
        "arg0": {
          "id": "idMock"
        }
      }
    ]
  },
  {
    "FakeObject.fakeMethodWithComplexObject": [
      {
        "arg0": {
          "id": "idMock",
          "name": "nameMock"
        }
      }
    ]
  }
]

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.

Inheritance

Test classes inheritance becames usefull with snapshot testing due to the fact that the assertions are variable following snasphots, instead of code. To make usage of this benefit you should be aware of the following:

Start SnapshotMatcher on child classes only:

package com.example;

import org.junit.AfterClass;
import org.junit.BeforeClass;

import static io.github.jsonSnapshot.SnapshotMatcher.start;
import static io.github.jsonSnapshot.SnapshotMatcher.validateSnapshots;

public class SnapshotChildClassTest extends SnapshotSuperClassTest {

    @BeforeClass
    public static void beforeAll() {
        start();
    }

    @AfterClass
    public static void afterAll() {
        validateSnapshots();
    }
    
    @Override
    public String getName() {
        return "anyName";
    }
}

Super classes can have @Test defined, but you should make the class abstract.

package com.example;

import org.junit.Test;

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

public abstract class SnapshotSuperClassTest {

    public abstract String getName();

    @Test
    public void shouldMatchSnapshotOne() {
        expect(getName()).toMatchSnapshot();
    }

}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].