rest-driver/rest-driver — Issue #108: How to use JSON matchers?
Author: marceloverdijk
Created: 2012-02-21 12:52:05+00:00
State: open

As there is no forum/mailing list available I'm just posting my question as issue.
I bumped against this library and it contains some useful Hamcrest Jackson JSON matchers.

I'm only wondering how to use them. Please have a look at below example unit test

```
package integration.resources;

import static com.github.restdriver.serverdriver.Json.asJson;
import static com.github.restdriver.serverdriver.Json.containingValue;
import static com.github.restdriver.serverdriver.Json.hasJsonArray;
import static com.github.restdriver.serverdriver.Json.hasJsonValue;
import static com.github.restdriver.serverdriver.Json.withSize;
import static com.github.restdriver.serverdriver.Matchers.hasJsonPath;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;

import org.codehaus.jackson.JsonNode;
import org.junit.Before;
import org.junit.Test;

import com.github.restdriver.matchers.HasJsonArray;

public class JsonTest {

private static final String JSON_STRING =
"{\"data\": {\"errors\": [\"Product.code.NotBlank\", \"Product.description.NotBlank\", \"Product.price.NotNull\"]}}";

private JsonNode json;

@Before
public void setUp() {
json = asJson(JSON_STRING);
}

@Test
public void testHasErrors() {
assertThat(json, hasJsonPath("data.errors"));
}

@Test
public void testErrorsIsArray() {
assertThat(json, hasJsonArray("data.errors", ??));
}

@Test
public void testErrorsContainsProductDescriptionNotBlank() {
assertThat(json, HasJsonArray("data.errors", containingValue(equalTo("Product.description.NotBlank"))));
}
}
```

The `testErrorsIsArray` and `testErrorsContainsProductDescriptionNotBlank` do not compile and I don't understand how to get this working.

Maybe I'm not taking the correct approach here...
What I would like to test is:
1) That the json contains an 'errors' property
2) That the 'errors' property is a (String) array
3) That the 'errors' array contains a String with value 'Product.description.NotBlank'

Any help will be appreciated.