### Question:
JsonPatch implements JsonSerializable { public JsonNode apply(final JsonNode node) { requireNonNull(node, "node"); JsonNode ret = node.deepCopy(); for (final JsonPatchOperation operation : operations) { ret = operation.apply(ret); } return ret; } @JsonCreator  JsonPatch(final List<JsonPatchOperation> operations); static JsonPatch fromJson(final JsonNode node); static JsonPatch generate(final JsonNode source, final JsonNode target, ReplaceMode replaceMode); boolean isEmpty(); List<JsonPatchOperation> operations(); JsonNode apply(final JsonNode node); ArrayNode toJson(); @Override String toString(); @Override void serialize(final JsonGenerator jgen, final SerializerProvider provider); @Override void serializeWithType(final JsonGenerator jgen,
                                  final SerializerProvider provider, final TypeSerializer typeSer);  }### Answer:
@Test void cannotPatchNull() { final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); assertThatNullPointerException() .isThrownBy(() -> patch.apply(null)); }
@Test void operationsAreCalledInOrder() { final JsonNode node1 = FACTORY.textNode("hello"); final JsonNode node2 = FACTORY.textNode("world"); when(op1.apply(node1)).thenReturn(node2); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); final ArgumentCaptor<JsonNode> captor = ArgumentCaptor.forClass(JsonNode.class); patch.apply(node1); verify(op1, only()).apply(same(node1)); verify(op2, only()).apply(captor.capture()); assertThat(captor.getValue()).isSameAs(node2); }
@Test void whenOneOperationFailsNextOperationIsNotCalled() { final String message = "foo"; when(op1.apply(any(JsonNode.class))) .thenThrow(new JsonPatchException(message)); final JsonPatch patch = new JsonPatch(ImmutableList.of(op1, op2)); assertThatThrownBy(() -> patch.apply(FACTORY.nullNode())) .isInstanceOf(JsonPatchException.class) .hasMessage(message); verifyNoMoreInteractions(op2); }