New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonClassPathSource support generics type? #639
Comments
Hi there @sherlockEix ! Just for the sake of completeness, could you also copy here your |
yes. this is my demo. i hope that
private static final String JEDIS = "org/junitpioneer/jupiter/json/jedis.json";
@ParameterizedTest
@JsonClasspathSource(JEDIS)
void listObject(List<Jedi> jedisList){
System.out.println("jedisList = " + jedisList);
}
[
{
"name": "Luke",
"height": 172
},
{
"name": "Yoda",
"height": 66
}
]
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<java.lang.Object>` from Object value (token `JsonToken.START_OBJECT`)
at [Source: UNKNOWN; byte offset: #UNKNOWN] |
Oh, yeah, I see where the problem is - you pass a list of objects to your test that expects a list of lists. See, when you execute a parameterized test, it expects an input (a list) which it converts to single parameters. In your example your parameter is a list, so the test tries to convert your input to a list but it can not do that because you supplied a simple list not a list of lists. To demonstrate with an example, this should work: @ParameterizedTest
@JsonClasspathSource(JEDI_LIST)
void listObject(List<Jedi> jedisList){
System.out.println("jedisList = " + jedisList);
} Jedi list: [
[
{
"name": "Luke",
"height": 172
},
{
"name": "Yoda",
"height": 66
}
]
] Note that I used two square brackets |
This actually makes me wonder why you would want to use a Again to demonstrate with an example, let's say you want to test a performance review system. This approach would be wrong: @ParameterizedTest
@EmployeeTeamsSource
void employeeScoresShouldExceedMinimum(List<Employee> employees) {
for (Employee employee : employees) {
assertThat(employee.performanceScore()).isGreaterThan(SCORE_MIN);
}
} In the above example you are testing individual objects, but you pass them in a list, which makes your test less readable. The correct approach in this case would be: @ParameterizedTest
@EmployeeSource
void employeeScoresShouldExceedMinimum(Employee employee) {
assertThat(employee.performanceScore()).isGreaterThan(SCORE_MIN);
} Which is basically the same test, except it lets the test unwrap your collection. This isn't always the right approach, of course. For example, you might want to evaluate the weighted average of performance scores in teams (a collection of employees). Then passing a @ParameterizedTest
@EmployeeTeamsSource
void employeeScoresShouldExceedMinimum(List<Employee> employees) {
assertThat(calculateWeightedAverage(employees)).isGreaterThan(SCORE_MIN);
} |
i want use |
Hello. I've tried the demo below but it doesn't work. I think
JsonClassPathSource
can't deserialize json array to java list. Is there any other way to implement it? Thanks.Java code:
Json file:
The text was updated successfully, but these errors were encountered: