junit-json-params icon indicating copy to clipboard operation
junit-json-params copied to clipboard

Kotlin support?

Open wbervoets opened this issue 4 years ago • 1 comments

I'm having a hard time to get this project to work on my JUnit 5 tests written in Kotlin. Is it supported / working?

@ParameterizedTest @JsonFileSource(resources = ["/my-json.json"]) fun test(myJSON: JSONObject) {} I'm receiving the following exception:

org.junit.jupiter.api.extension.ParameterResolutionException: Error converting parameter at index 0: Can't convert to type: 'org.json.JSONObject'

at org.junit.jupiter.params.ParameterizedTestMethodContext.parameterResolutionException(ParameterizedTestMethodContext.java:273)
at org.junit.jupiter.params.ParameterizedTestMethodContext.access$200(ParameterizedTestMethodContext.java:44)
at org.junit.jupiter.params.ParameterizedTestMethodContext$Converter.resolve(ParameterizedTestMethodContext.java:238)
at org.junit.jupiter.params.ParameterizedTestMethodContext.resolve(ParameterizedTestMethodContext.java:165)
at org.junit.jupiter.params.ParameterizedTestParameterResolver.resolveParameter(ParameterizedTestParameterResolver.java:64)
at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:216)
at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameters(ExecutableInvoker.java:183)
at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameters(ExecutableInvoker.java:144)
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:96)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:212)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:208)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
at org.junit.platform.engine.support.hierarchical.ForkJoinPoolHierarchicalTestExecutorService$ExclusiveTask.compute(ForkJoinPoolHierarchicalTestExecutorService.java:171)
at java.util.concurrent.RecursiveAction.exec(RecursiveAction.java:189)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

Caused by: org.junit.jupiter.params.converter.ArgumentConversionException: Can't convert to type: 'org.json.JSONObject' at net.joshka.junit.json.params.JsonConverter.convert(JsonConverter.java:38) at org.junit.jupiter.params.ParameterizedTestMethodContext$Converter.resolve(ParameterizedTestMethodContext.java:235) ... 25 more

When I put an exception breakpoint I see that the JSONObject is correctly filled in, but because JsonConverter only knows how to handle strings, int and booleans it throws the exception.

I tried changing the parameter type to string so it would do jsonObject.getString("keyName") but keyname is always "arg0" and that key name does not exist in my JSON file.

Any idea?

Thanks Wim

I'm using the latest version of JUnit 5.6.1

wbervoets avatar Mar 31 '20 15:03 wbervoets

I don't have any Kotlin experience, and I'm not currently actively using this library on any of my current projects (which are mostly JUnit4 based 😭). Happy to take pull requests / perform builds as necessary though.

Regarding "arg0" / JSONObject, I'm unsure. Can you repro this with a small test case in java (feel free to submit it as a PR and then attempt a fix). I'm also happy to accept some example Kotlin tests if you have ideas about how best to add them to the lib.

Re: JUnit versioning - I haven't bumped a new version out for 5.6 yet as it's a couple of steps and I haven't automated this. I'm assuming that things just work, but there also didn't look like there was anything obviously incompatible from the junit release notes.

joshka avatar Mar 31 '20 20:03 joshka

Closing. See https://github.com/joshka/junit-json-params/issues/177 for more info.

joshka avatar Aug 20 '24 10:08 joshka