Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This extension needs a JSON parser. In order not do pull in a specific run-time dependency, it makes use of Gradle's feature variants as described in CONTRIBUTING.md (including Checkstyle rules). For now, only Jackson is supported as JSON parser. Other parsers can be added in future versions if users need them. Closes: #101 PR: #492
- Loading branch information
Showing
37 changed files
with
2,418 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
:page-title: JSON Argument Source | ||
:page-description: Extends JUnit Jupiter with `@JsonFileSource`, a parametrized test that creates test based on a JSON Source | ||
:xp-demo-dir: ../src/demo/java | ||
:json-demo: {xp-demo-dir}/org/junitpioneer/jupiter/json/JsonArgumentSourceExtensionDemo.java | ||
:jedi: {xp-demo-dir}/org/junitpioneer/jupiter/json/Jedi.java | ||
|
||
The JSON argument sources let you provide arguments for parameterized tests from JSON. | ||
There are three annotations: | ||
|
||
* `@JsonSource` for lenient inline JSON | ||
* `@JsonFileSource` for JSON files from the local file system | ||
* `@JsonClasspathSource` for JSON files from the classpath | ||
There are various ways how the method arguments for a single parametrized test are provided. | ||
By default the root of the source will be treated as candidate for the test arguments. | ||
If the root is an object then the entire object will be one argument, if the root is an array then every element of the array will be one argument. | ||
|
||
It is also possible to use a nested array from the provided JSON to access the source for the test arguments. | ||
The `JsonFileSource#data` can be used to tell the extraction mechanism to use the element with that name to look for the source of the data. | ||
|
||
Depending on the test method parameters, the extraction of the values might differ. | ||
|
||
== Method Arguments | ||
|
||
=== Single Argument Methods | ||
|
||
If the method has a single argument, the JSON object argument will be converted to that type. | ||
|
||
.Argument type | ||
[source,java] | ||
---- | ||
include::{jedi}[tag=class] | ||
---- | ||
|
||
.JSON Source File | ||
[source,json] | ||
---- | ||
[ | ||
{ | ||
"name": "Luke", | ||
"height": 172 | ||
}, | ||
{ | ||
"name": "Yoda", | ||
"height": 66 | ||
} | ||
] | ||
---- | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}[tag=classpath_source,indent=0] | ||
---- | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}[tag=inline_source,indent=0] | ||
---- | ||
|
||
This parametrized test will generate the following test executions: | ||
|
||
* [1] Jedi {name='Luke', height=172} | ||
* [2] Jedi {name='Yoda', height=66} | ||
|
||
It is also possible to extract only a single element from each argument object by using the `@Property` annotation. | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}/[tag=classpath_source_with_property,indent=0] | ||
---- | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}[tag=inline_source_with_property,indent=0] | ||
---- | ||
|
||
This parametrized test will generate the following tests: | ||
|
||
* [1] Luke | ||
* [2] Yoda | ||
|
||
=== Multiple Argument Methods | ||
|
||
If the method has multiple arguments, each JSON object argument will be deconstructed to each of the method arguments. | ||
By default, the method argument name will be used for locating the element that needs to be taken from the JSON object. | ||
You can also use `@Property` to give the name of the element that needs to be extracted. | ||
|
||
[IMPORTANT] | ||
==== | ||
If your test sources are not compiled using the `--parameters` flag then the names of the arguments will not be like they are written in the source code. | ||
In that the situation you need to use `@Property` instead. | ||
==== | ||
|
||
Using the same `jedis.json` and the following test | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}[tag=classpath_source_deconstruct_from_array,indent=0] | ||
---- | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}[tag=inline_source_deconstruct_from_array,indent=0] | ||
---- | ||
|
||
This parametrized test will generate the following tests: | ||
|
||
* [1] Luke, 172 | ||
* [2] Yoda, 66 | ||
|
||
== Extracting nested array | ||
|
||
Sometimes we want to extract a nested array instead of the root element. | ||
For this purpose `JsonClasspathSource#data` can be used. | ||
|
||
.Jedi with nested array | ||
[source,json] | ||
---- | ||
{ | ||
"name": "Luke", | ||
"height": 172, | ||
"vehicles": [ | ||
{ | ||
"name": "Snowspeeder", | ||
"length": 4.5 | ||
}, | ||
{ | ||
"name": "Imperial Speeder Bike", | ||
"length": 3 | ||
} | ||
] | ||
} | ||
---- | ||
|
||
Here we want to test the vehicles. | ||
The test for this will look like: | ||
|
||
[source,java] | ||
---- | ||
include::{json-demo}[tag=classpath_source_nested_data,indent=0] | ||
---- | ||
|
||
This parametrized test will generate the following tests: | ||
|
||
* [1] Snowspeeder, 4.5 | ||
* [2] Imperial Speeder Bike, 3 | ||
|
||
== Thread-Safety | ||
|
||
This extension is safe to use during https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution[parallel test execution]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* Copyright 2016-2021 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.json; | ||
|
||
// tag::class[] | ||
public class Jedi { | ||
|
||
public String name; | ||
public String height; | ||
|
||
@Override | ||
public String toString() { | ||
return "Jedi {" + "name='" + name + '\'' + ", height=" + height + '}'; | ||
} | ||
|
||
} | ||
// end::class[] |
99 changes: 99 additions & 0 deletions
99
src/demo/java/org/junitpioneer/jupiter/json/JsonArgumentSourceExtensionDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2016-2021 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.json; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
|
||
class JsonArgumentSourceExtensionDemo { | ||
|
||
@Nested | ||
class ClasspathDemo { | ||
|
||
// tag::classpath_source[] | ||
@ParameterizedTest | ||
@JsonClasspathSource("jedis.json") | ||
void singleJedi(Jedi jedi) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// end::classpath_source[] | ||
|
||
// tag::classpath_source_with_property[] | ||
@ParameterizedTest | ||
@JsonClasspathSource("jedis.json") | ||
void singleJediProperty(@Property("name") String jediName) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// end::classpath_source_with_property[] | ||
|
||
// tag::classpath_source_deconstruct_from_array[] | ||
@ParameterizedTest | ||
@JsonClasspathSource("jedis.json") | ||
void deconstructFromArray(@Property("name") String name, @Property("height") int height) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// end::classpath_source_deconstruct_from_array[] | ||
|
||
// tag::classpath_source_nested_data[] | ||
@ParameterizedTest | ||
@JsonClasspathSource(value = "luke.json", data = "vehicles") | ||
void lukeVehicles(@Property("name") String name, @Property("length") double length) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// end::classpath_source_nested_data[] | ||
|
||
} | ||
|
||
@Nested | ||
class InlineDemo { | ||
|
||
// @formatter:off | ||
// tag::inline_source[] | ||
@ParameterizedTest | ||
@JsonSource("[" | ||
+ " { name: 'Luke', height: 172 }," | ||
+ " { name: 'Yoda', height: 66 }" | ||
+ "]") | ||
void singleJedi(Jedi jedi) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// end::inline_source[] | ||
// @formatter:on | ||
|
||
// @formatter:off | ||
// tag::inline_source_with_property[] | ||
@ParameterizedTest | ||
@JsonSource({ | ||
"{ name: 'Luke', height: 172 }", | ||
"{ name: 'Yoda', height: 66 }" | ||
}) | ||
void singleJediProperty(@Property("name") String jediName) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// end::inline_source_with_property[] | ||
// @formatter:on | ||
|
||
// @formatter:off | ||
// tag::inline_source_deconstruct_from_array[] | ||
@ParameterizedTest | ||
@JsonSource({ | ||
"{ name: 'Yoda', height: 66 }", | ||
"{ name: 'Luke', height: 172 }", | ||
}) | ||
void deconstructFromArray(@Property("name") String name, @Property("height") int height) { | ||
// YOUR TEST CODE HERE | ||
} | ||
// @formatter:on | ||
// end::inline_source_deconstruct_from_array[] | ||
|
||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
src/demo/java/org/junitpioneer/jupiter/json/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* Package containing demonstration tests for corresponding package in the main project. | ||
*/ | ||
|
||
package org.junitpioneer.jupiter.json; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"name": "Luke", | ||
"height": 172 | ||
}, | ||
{ | ||
"name": "Yoda", | ||
"height": 66 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "Luke", | ||
"height": 172, | ||
"vehicles": [ | ||
{ | ||
"name": "Snowspeeder", | ||
"length": 4.5 | ||
}, | ||
{ | ||
"name": "Imperial Speeder Bike", | ||
"length": 3 | ||
} | ||
] | ||
} |
Oops, something went wrong.