Skip to content

Commit

Permalink
Enable int, bool, String method parameter conversion
Browse files Browse the repository at this point in the history
Introduce JsonConverter that converts a single parameter by name from
Json to the parameter type (String, int, or boolean).
Only single parameters are supported right now.

#3
  • Loading branch information
joshka committed Dec 1, 2018
1 parent 156e443 commit b47c637
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions 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'
Expand All @@ -19,6 +22,7 @@ targetCompatibility = 1.8

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << '-parameters'
}

repositories {
Expand Down Expand Up @@ -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"
}
}
}
40 changes: 40 additions & 0 deletions 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() + "'");
}
}
@@ -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);
}
}

0 comments on commit b47c637

Please sign in to comment.