Skip to content

Commit b47c637

Browse files
committed
Enable int, bool, String method parameter conversion
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
1 parent 156e443 commit b47c637

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## Unreleased
9+
### Added
10+
- Use @JsonConverter to provide actual arguments to tests
11+
812
## [1.3.2] - 2018-11-29
913
### Changed
1014
- Updated to JUnit 5.3.2

build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
plugins {
22
id "org.sonarqube" version "2.6"
3+
id 'idea'
4+
id "org.jetbrains.gradle.plugin.idea-ext" version "0.4.2"
35
}
46

57
apply plugin: 'java-library'
68
apply plugin: 'jacoco'
79
apply plugin: 'maven-publish'
810
apply plugin: 'signing'
11+
apply plugin: 'idea'
912

1013
group = 'net.joshka'
1114
version = '1.3.2'
@@ -19,6 +22,7 @@ targetCompatibility = 1.8
1922

2023
tasks.withType(JavaCompile) {
2124
options.encoding = 'UTF-8'
25+
options.compilerArgs << '-parameters'
2226
}
2327

2428
repositories {
@@ -116,3 +120,12 @@ signing {
116120
useGpgCmd()
117121
sign publishing.publications.mavenJava
118122
}
123+
124+
idea.project.settings {
125+
compiler {
126+
javac {
127+
// Necessary for JsonConverter as method parameters otherwise have names like 'arg0', ...
128+
javacAdditionalOptions "-parameters"
129+
}
130+
}
131+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package net.joshka.junit.json.params;
2+
3+
import org.junit.jupiter.api.extension.ParameterContext;
4+
import org.junit.jupiter.params.converter.ArgumentConversionException;
5+
import org.junit.jupiter.params.converter.ArgumentConverter;
6+
7+
import javax.json.JsonObject;
8+
9+
public class JsonConverter implements ArgumentConverter {
10+
11+
/**
12+
* Convert the supplied {@code source} object according to the supplied
13+
* {@code context}.
14+
*
15+
* @param source the source object to convert; may be {@code null}
16+
* @param context the parameter context where the converted object will be
17+
* used; never {@code null}
18+
* @return the converted object; may be {@code null} but only if the target
19+
* type is a reference type
20+
* @throws ArgumentConversionException if an error occurs during the
21+
* conversion
22+
*/
23+
@Override
24+
public Object convert(Object source, ParameterContext context) {
25+
if (!(source instanceof JsonObject)) {
26+
throw new ArgumentConversionException("Not a JsonObject");
27+
}
28+
JsonObject json = (JsonObject) source;
29+
String name = context.getParameter().getName();
30+
Class<?> type = context.getParameter().getType();
31+
if (type == String.class) {
32+
return json.getString(name);
33+
} else if (type == int.class) {
34+
return json.getInt(name);
35+
} else if (type == boolean.class) {
36+
return json.getBoolean(name);
37+
}
38+
throw new ArgumentConversionException("Can't convert to type: '" + type.getName() + "'");
39+
}
40+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package net.joshka.junit.json.params;
2+
3+
import org.junit.jupiter.api.Disabled;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.converter.ConvertWith;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
class JsonArgumentsToValuesTest {
12+
@ParameterizedTest
13+
@JsonSource("{'key':'value'}")
14+
@DisplayName("Converts to Strings")
15+
void strings(@ConvertWith(JsonConverter.class) String key) {
16+
assertEquals("value", key);
17+
}
18+
19+
@ParameterizedTest
20+
@JsonSource("{'key': true}")
21+
@DisplayName("Converts to booleans")
22+
void ints(@ConvertWith(JsonConverter.class) boolean key) {
23+
assertTrue(key);
24+
}
25+
26+
27+
@ParameterizedTest
28+
@JsonSource("{'key': 1 }")
29+
@DisplayName("Converts to ints")
30+
void booleans(@ConvertWith(JsonConverter.class) int key) {
31+
assertEquals(1, key);
32+
}
33+
34+
@ParameterizedTest
35+
@Disabled("Doesn't yet work as only the first parameter gets a value")
36+
@JsonSource("{'stringKey':'value', 'boolKey': true, 'intKey': 1 }")
37+
@DisplayName("provides multiple objects")
38+
void multipleObjects(
39+
@ConvertWith(JsonConverter.class) String stringKey,
40+
@ConvertWith(JsonConverter.class) int intKey,
41+
@ConvertWith(JsonConverter.class) boolean boolKey) {
42+
assertEquals("value", stringKey);
43+
assertTrue(boolKey);
44+
assertEquals(1, intKey);
45+
}
46+
}

0 commit comments

Comments
 (0)