2

Is there any existing solution for doing snapshot testing [1] in Spring MVC test (MockMvc)?

Something like:

this.mockMvc.perform(get("/users")
  .andExpect(status().isOk())
  .andExpect(content().contentType("application/json"))
  .andExpect(matchesSnapshot("__snapshots__/users/list.json"));

Running this test first time would make test pass and write response content to src/main/resources/__snapshots__/users/list.json.

Running this test second time compares response content to src/main/resources/__snapshots__/users/list.json. If it's the same, it passes. Otherwise, it fails.

[1] https://jestjs.io/docs/en/snapshot-testing

Matija Folnovic
  • 906
  • 2
  • 10
  • 22

1 Answers1

0

https://github.com/Zenika/java-snapshot-matcher

Snapshot testing with Java:

We use Jackson to handle serialization/deserialization to JSON format. This library therefore can handle any object that is serializable by Jackson.

Real world example:

I want to test a converter, which transforms a Planet into a PlanetDTO.

@Component
public class PlanetConverter {

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RestTemplate restTemplate;

    public PlanetDTO convertPlanet(Planet planet) {
        PlanetDTO dto = objectMapper.convertValue(planet, PlanetDTO.class);

        dto.films = planet.filmsUrls.stream()
                .map(filmUrl -> restTemplate.getForObject(filmUrl, FilmDTO.class))
                .collect(Collectors.toList());

        dto.residents = planet.residentsUrls.stream()
                .map(filmUrl -> restTemplate.getForObject(filmUrl, PeopleDTO.class))
                .collect(Collectors.toList());

        return dto;
    }
}

As it can be difficult to generate input object and to perform assertions on returned object, we use the snapshot matcher to handle assertions.

@RunWith(SpringRunner.class)
@SpringBootTest
public class PlanetConverterTest {

    // RestTemplate needs to be mocked to ensure stability of snapshots.
    // Note that ObjectMapper is not mocked
    @Mock
    private RestTemplate restTemplate;

    @Autowired
    @InjectMocks
    private PlanetConverter converter;

    // Create input objects from JSON files
    private PeopleDTO lukeSkywalker = fromJson("luke-skywalker", PeopleDTO.class);
    private FilmDTO attackOfTheClones = fromJson("attack-of-the-clones", FilmDTO.class);
    private Planet tatooine = fromJson("tatooine", Planet.class);

    @Before
    public void setUp() {
        // Mock return values of RestTemplate
        when(restTemplate.getForObject("https://swapi.co/api/people/1/", PeopleDTO.class))
                .thenReturn(lukeSkywalker);
        when(restTemplate.getForObject("https://swapi.co/api/films/5/", FilmDTO.class))
                .thenReturn(attackOfTheClones);
    }

    @Test
    public void converterTest() {
        // Call method
        PlanetDTO planet = converter.convertPlanet(tatooine);

        // Do assertion
        assertThat(planet, matchesSnapshot());
    }
}
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22