Hamcrest with MockMvc: check that key exists but value may be null
Asked Answered
E

4

22

I'm doing some tests with MockMvc, and I want to validate the structure of a JSON response. Specifically, I want to make sure that the key to an attribute exists, and that the value is of a certain type or null.

{
   "keyToNull": null,  # This may be null, or a String
   "keyToString": "some value"
}

The following works for me, but I'm wondering if there's a way to combine each group of two expectations into a single line, as I have a lot of attributes to check:

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.hamcrest.Matchers.*;

.andExpect(jsonPath("$").value(hasKey("keyToNull")))
.andExpect(jsonPath("$.keyToNull").value(
                  anyOf(any(String.class), nullValue(String.class))))

.andExpect(jsonPath("$").value(hasKey("keyToString")))
.andExpect(jsonPath("$.keyToString").value(
                  anyOf(any(String.class), nullValue(String.class))))

The hasKey() is necessary because the other assertion by itself passes since nonexistent keys in MockMvc's implementation map to null:

.andExpect(jsonPath("$.notAKey").value(
                  anyOf(any(String.class), nullValue(String.class)))) // ok

jsonPath().exists() also doesn't work, because internally it compares the value against null.

I've considered making a separate method like this:

private static <T> void assertNullableAttr(ResultActions res, String jsonPath, Class<T> type) throws Exception {
    int split = jsonPath.lastIndexOf('.');
    String prefix = jsonPath.substring(0, split), key = jsonPath.substring(split+1);

    res.andExpect(jsonPath(prefix).value(hasKey(key)))
        .andExpect(jsonPath(jsonPath).value(anyOf(any(type), nullValue(type))));
    }

But then it forces me to split my code in an unnatural way:

ResultActions res = mockMvc.perform(get("/api"))
    // these attributes must not be null
    .andExpect(jsonPath("$.someInfo").value(hasSize(2)))
        .andExpect(jsonPath("$.someInfo[0].info1").value(any(String.class)))
        .andExpect(jsonPath("$.someInfo[0].info2").value(any(String.class)))
    .andExpect(jsonPath("$.addlInfo").value(hasSize(2)))
        .andExpect(jsonPath("$.addlInfo[0].info1").value(any(String.class)))
        .andExpect(jsonPath("$.addlInfo[0].info2").value(any(String.class)));

// these attributes may be null
assertNullableAttr(res, "$.someInfo[0].info3", String.class);
assertNullableAttr(res, "$.someInfo[0].info4", String.class);

assertNullableAttr(res, "$.addlInfo[0].info3", String.class);
assertNullableAttr(res, "$.addlInfo[0].info4", String.class);

Is there any clever Hamcrest Matcher I could apply to a single json path per attribute?

Episodic answered 14/8, 2014 at 22:16 Comment(0)
E
8

You can add the following static matcher factory:

public static <K> Matcher<Map<? extends K, ?>> hasNullKey(K key) {
    return new IsMapContaining<K,Object>(equalTo(key), anyOf(nullValue(), anyString());
}

And then, you can use it like this:

// will succeed, because keyToNull exists and null
.andExpect(jsonPath("$").value(hasNullKey("keyToNull")))

// will succeed, bacause keyToString exists and not null
.andExpect(jsonPath("$").value(hasNullKey("keyToString")))

// will fail, because notAKey doesn't exists
.andExpect(jsonPath("$").value(hasNullKey("notAKey")))
Ephemerid answered 20/10, 2014 at 12:46 Comment(0)
C
28

You can perform this operation with the following existing test classes:

.andExpect(jsonPath("$..myExpectedNullKey[0]").value(IsNull.nullValue()));

Be sure to import org.hamcrest.core.IsNull

Cockerel answered 18/2, 2015 at 18:38 Comment(0)
P
26

I have found this solution in this blog:

.andExpect(jsonPath( "$.keyToNull").doesNotExist());

It works for me.

Protuberance answered 12/9, 2017 at 15:13 Comment(0)
E
8

You can add the following static matcher factory:

public static <K> Matcher<Map<? extends K, ?>> hasNullKey(K key) {
    return new IsMapContaining<K,Object>(equalTo(key), anyOf(nullValue(), anyString());
}

And then, you can use it like this:

// will succeed, because keyToNull exists and null
.andExpect(jsonPath("$").value(hasNullKey("keyToNull")))

// will succeed, bacause keyToString exists and not null
.andExpect(jsonPath("$").value(hasNullKey("keyToString")))

// will fail, because notAKey doesn't exists
.andExpect(jsonPath("$").value(hasNullKey("notAKey")))
Ephemerid answered 20/10, 2014 at 12:46 Comment(0)
H
2

Here my solution:

.andExpect(jsonPath("$.object", is(nullValue())));

Hope this helps!

Homovec answered 28/11, 2019 at 15:49 Comment(1)
where nullValue() and is() come from? Answers should be more precise!Pham

© 2022 - 2024 — McMap. All rights reserved.