diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java index 47e5f9df..9ee6f63c 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/DefaultGraphQlTester.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -336,8 +336,7 @@ class DefaultGraphQlTester implements GraphQlTester { @Override public PathSpec pathDoesNotExist() { - this.responseContainer - .doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.responseContainer.jsonContent())); + this.responseContainer.doAssert(() -> this.pathHelper.doesNotHaveJsonPath(this.responseContainer.jsonContent())); return this; } @@ -355,21 +354,13 @@ class DefaultGraphQlTester implements GraphQlTester { @Override public PathSpec valueIsEmpty() { - this.responseContainer.doAssert(() -> { - try { - this.pathHelper.assertValueIsEmpty(this.responseContainer.jsonContent()); - } - catch (AssertionError ex) { - // ignore - } - }); + this.responseContainer.doAssert(() -> this.pathHelper.assertValueIsEmpty(this.responseContainer.jsonContent())); return this; } @Override public PathSpec valueIsNotEmpty() { - this.responseContainer - .doAssert(() -> this.pathHelper.assertValueIsNotEmpty(this.responseContainer.jsonContent())); + this.responseContainer.doAssert(() -> this.pathHelper.assertValueIsNotEmpty(this.responseContainer.jsonContent())); return this; } diff --git a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java index cd575a6b..080e0355 100644 --- a/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java +++ b/spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/GraphQlTester.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -249,15 +249,15 @@ public interface GraphQlTester { PathSpec valueDoesNotExist(); /** - * Assert the value at the given path does not exist or is empty as defined in - * {@link org.springframework.util.ObjectUtils#isEmpty(Object)}. + * Assert the value at the given path does exist but is empty as defined + * in {@link org.springframework.util.ObjectUtils#isEmpty(Object)}. * @return spec to assert the converted entity with * @see org.springframework.util.ObjectUtils#isEmpty(Object) */ PathSpec valueIsEmpty(); /** - * Assert the value at the given path is not {@link #valueIsEmpty()}. + * Assert the value at the given path is not {@link #valueIsEmpty() empty}. * @return spec to assert the converted entity with */ PathSpec valueIsNotEmpty(); diff --git a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java index 866bd299..12bd377b 100644 --- a/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java +++ b/spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/GraphQlTesterTests.java @@ -66,16 +66,34 @@ public class GraphQlTesterTests { @Test - void pathAndValueExistsAndEmptyChecks() throws Exception { + void pathAndValueExist() throws Exception { String query = "{me {name, friends}}"; setResponse("{\"me\": {\"name\":\"Luke Skywalker\", \"friends\":[]}}"); GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute(); - spec.path("me.name").pathExists().valueExists().valueIsNotEmpty(); + spec.path("me.name").pathExists().valueExists(); + spec.path("me.friends").pathExists().valueExists(); + spec.path("hero").pathDoesNotExist().valueDoesNotExist(); + + assertThat(this.inputCaptor.getValue().getQuery()).contains(query); + } + + @Test + void valueIsEmpty() throws Exception { + + String query = "{me {name, friends}}"; + setResponse("{\"me\": {\"name\":null, \"friends\":[]}}"); + + GraphQlTester.ResponseSpec spec = this.graphQlTester.query(query).execute(); + + spec.path("me.name").valueIsEmpty(); spec.path("me.friends").valueIsEmpty(); - spec.path("hero").pathDoesNotExist().valueDoesNotExist().valueIsEmpty(); + + assertThatThrownBy(() -> spec.path("hero").valueIsEmpty()) + .as("Path does not even exist") + .hasMessageContaining("No value at JSON path \"$['data']['hero']"); assertThat(this.inputCaptor.getValue().getQuery()).contains(query); }