diff --git a/CHANGELOG.md b/CHANGELOG.md index 95bd499..0b81c42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Added +- Use @JsonConverter to provide actual arguments to tests + ## [1.3.2] - 2018-11-29 ### Changed - Updated to JUnit 5.3.2 diff --git a/build.gradle b/build.gradle index b2ad7d6..7ada8fd 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,14 @@ plugins { id "org.sonarqube" version "2.6" + id 'idea' + id "org.jetbrains.gradle.plugin.idea-ext" version "0.4.2" } apply plugin: 'java-library' apply plugin: 'jacoco' apply plugin: 'maven-publish' apply plugin: 'signing' +apply plugin: 'idea' group = 'net.joshka' version = '1.3.2' @@ -19,6 +22,7 @@ targetCompatibility = 1.8 tasks.withType(JavaCompile) { options.encoding = 'UTF-8' + options.compilerArgs << '-parameters' } repositories { @@ -116,3 +120,12 @@ signing { useGpgCmd() sign publishing.publications.mavenJava } + +idea.project.settings { + compiler { + javac { + // Necessary for JsonConverter as method parameters otherwise have names like 'arg0', ... + javacAdditionalOptions "-parameters" + } + } +} \ No newline at end of file diff --git a/src/main/java/net/joshka/junit/json/params/JsonConverter.java b/src/main/java/net/joshka/junit/json/params/JsonConverter.java new file mode 100644 index 0000000..0d98da1 --- /dev/null +++ b/src/main/java/net/joshka/junit/json/params/JsonConverter.java @@ -0,0 +1,40 @@ +package net.joshka.junit.json.params; + +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.params.converter.ArgumentConversionException; +import org.junit.jupiter.params.converter.ArgumentConverter; + +import javax.json.JsonObject; + +public class JsonConverter implements ArgumentConverter { + + /** + * Convert the supplied {@code source} object according to the supplied + * {@code context}. + * + * @param source the source object to convert; may be {@code null} + * @param context the parameter context where the converted object will be + * used; never {@code null} + * @return the converted object; may be {@code null} but only if the target + * type is a reference type + * @throws ArgumentConversionException if an error occurs during the + * conversion + */ + @Override + public Object convert(Object source, ParameterContext context) { + if (!(source instanceof JsonObject)) { + throw new ArgumentConversionException("Not a JsonObject"); + } + JsonObject json = (JsonObject) source; + String name = context.getParameter().getName(); + Class type = context.getParameter().getType(); + if (type == String.class) { + return json.getString(name); + } else if (type == int.class) { + return json.getInt(name); + } else if (type == boolean.class) { + return json.getBoolean(name); + } + throw new ArgumentConversionException("Can't convert to type: '" + type.getName() + "'"); + } +} diff --git a/src/test/java/net/joshka/junit/json/params/JsonArgumentsToValuesTest.java b/src/test/java/net/joshka/junit/json/params/JsonArgumentsToValuesTest.java new file mode 100644 index 0000000..cc14aef --- /dev/null +++ b/src/test/java/net/joshka/junit/json/params/JsonArgumentsToValuesTest.java @@ -0,0 +1,46 @@ +package net.joshka.junit.json.params; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.converter.ConvertWith; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class JsonArgumentsToValuesTest { + @ParameterizedTest + @JsonSource("{'key':'value'}") + @DisplayName("Converts to Strings") + void strings(@ConvertWith(JsonConverter.class) String key) { + assertEquals("value", key); + } + + @ParameterizedTest + @JsonSource("{'key': true}") + @DisplayName("Converts to booleans") + void ints(@ConvertWith(JsonConverter.class) boolean key) { + assertTrue(key); + } + + + @ParameterizedTest + @JsonSource("{'key': 1 }") + @DisplayName("Converts to ints") + void booleans(@ConvertWith(JsonConverter.class) int key) { + assertEquals(1, key); + } + + @ParameterizedTest + @Disabled("Doesn't yet work as only the first parameter gets a value") + @JsonSource("{'stringKey':'value', 'boolKey': true, 'intKey': 1 }") + @DisplayName("provides multiple objects") + void multipleObjects( + @ConvertWith(JsonConverter.class) String stringKey, + @ConvertWith(JsonConverter.class) int intKey, + @ConvertWith(JsonConverter.class) boolean boolKey) { + assertEquals("value", stringKey); + assertTrue(boolKey); + assertEquals(1, intKey); + } +}