jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject [jmeter] 13/13: Use parameterized tests to lessen repetitive assertions
Date Sat, 10 Oct 2020 11:29:14 GMT
This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git

commit ba7a8c7eb6e95b20370767748a54cb2c5d00c3cc
Author: Felix Schumacher <felix.schumacher@internetallee.de>
AuthorDate: Sat Oct 10 13:18:09 2020 +0200

    Use parameterized tests to lessen repetitive assertions
---
 .../http/util/TestGraphQLRequestParamUtils.java    | 38 +++++++++++++---------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java
b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java
index 70860f1..de738a7 100644
--- a/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java
+++ b/src/protocol/http/src/test/java/org/apache/jmeter/protocol/http/util/TestGraphQLRequestParamUtils.java
@@ -25,12 +25,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.StandardCharsets;
+import java.util.stream.Stream;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.jmeter.config.Arguments;
 import org.apache.jmeter.protocol.http.config.GraphQLRequestParams;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 
@@ -75,16 +79,20 @@ public class TestGraphQLRequestParamUtils {
         params = new GraphQLRequestParams(OPERATION_NAME, QUERY, VARIABLES);
     }
 
-    @Test
-    void testIsGraphQLContentType() throws Exception {
-        assertTrue(GraphQLRequestParamUtils.isGraphQLContentType("application/json"));
-        assertTrue(GraphQLRequestParamUtils.isGraphQLContentType("application/json;charset=utf-8"));
-        assertTrue(GraphQLRequestParamUtils.isGraphQLContentType("application/json; charset=utf-8"));
-
-        assertFalse(GraphQLRequestParamUtils.isGraphQLContentType("application/vnd.api+json"));
-        assertFalse(GraphQLRequestParamUtils.isGraphQLContentType("application/json-patch+json"));
-        assertFalse(GraphQLRequestParamUtils.isGraphQLContentType(""));
-        assertFalse(GraphQLRequestParamUtils.isGraphQLContentType(null));
+    @ParameterizedTest
+    @ValueSource(strings = { "application/json", "application/json;charset=utf-8", "application/json;
charset=utf-8" })
+    void testIsGraphQLContentType(String contentType) throws Exception {
+        assertTrue(GraphQLRequestParamUtils.isGraphQLContentType(contentType));
+    }
+
+    // null can't be used in a ValueSource directly, so we need to use a MethodSource
+    static Stream<String> invalidContentTypes = Stream.of("application/vnd.api+json",
"application/json-patch+json", "",
+            null);
+
+    @ParameterizedTest
+    @MethodSource("invalidContentTypes")
+    void testInvalidGraphQLContentType(String contentType) {
+        assertFalse(GraphQLRequestParamUtils.isGraphQLContentType(contentType));
     }
 
     @Test
@@ -120,13 +128,11 @@ public class TestGraphQLRequestParamUtils {
         assertEquals("{\"id\":123}", params.getVariables());
     }
 
-    @Test
-    void testInvalidJsonData() throws JsonProcessingException, UnsupportedEncodingException
{
-        assertThrows(IllegalArgumentException.class,
-                () -> GraphQLRequestParamUtils.toGraphQLRequestParams("".getBytes(StandardCharsets.UTF_8),
null));
-
+    @ParameterizedTest
+    @ValueSource(strings = { "", "{}"})
+    void testInvalidJsonData(String postData) throws JsonProcessingException, UnsupportedEncodingException
{
         assertThrows(IllegalArgumentException.class,
-                () -> GraphQLRequestParamUtils.toGraphQLRequestParams("{}".getBytes(StandardCharsets.UTF_8),
null));
+                () -> GraphQLRequestParamUtils.toGraphQLRequestParams(postData.getBytes(StandardCharsets.UTF_8),
null));
     }
 
     @Test


Mime
View raw message