Closed Takiguchi72 closed 7 months ago
Could you provide a simple case to reproduce the behaviours you are finding erroneous? It looks like you have several libraries going on here that could each be responsible.
Internally a call to isOk
is checking if the inlined value is an instanceof Failure
, which is what the Err
function wraps your value in. The only time that should return false
is if you haven't called Err
somewhere.
Hello, thanks for your reply.
I built a sample gradle project that contains some code to reproduce my bug with the kotlin-result library.
You'll find it there: https://github.com/Takiguchi72/kotlin-result-issue-102-example
You only have to run this command:
./gradlew build
Or run the parameterized unit test in class OneServiceTest
if you open the project in an IDE like IntelliJ.
You'll find a link to a report with the error stack trace in the terminal:
Thanks in advance
This seems to be a bug with JUnit's parameterised tests, almost certainly related to https://github.com/junit-team/junit5/issues/2703.
You can confirm this yourself with the following experiment:
@Nested
inner class DoSomething {
@ParameterizedTest
@MethodSource("com.example.demo.OneServiceTest#argumentsOfMyTest")
fun `test name`(firstResult: Result<Customer, CustomerError>) {
oneService.doSomething()
}
}
private companion object {
@JvmStatic
fun argumentsOfMyTest() = Stream.of(
Arguments.of(
Err(CustomerError("")),
)
)
}
If you look at the test above in the debugger, JUnit is actually wrapping your Err(CustomerError(""))
in another Result
, that is itself Ok
:
See how the watch on firstResult.toString()
is telling us that it's actually an Ok
wrapping an Err
? This means the outer Result
is Ok
, but the value that the outer Result
holds is another Result
that is an Err
. It seems like JUnit is magically wrapping the Err(CustomerError(""))
with another Result
, which is itself therefore Ok
.
Interestingly, you can stop JUnit doing this magic behaviour by typing your argument as an Any
, notice in the screenshot below that it hasn't magically wrapped it with another Result
:
Therefore the behaviour you've identified that made it look like an Err
was returning true
for isOk
is not accurate, JUnit has magically wrapped your Err
in another Ok
, which itself isOk
as we would expect. I don't know how you would opt out of this behaviour in any way other than making your arguments a typeof Any
to avoid this weird casting behaviour. I suspect this is something you'll have to ask the JUnit team to opt out of.
Thanks for your analyze, I'll dig up the Junit thread because source problem is not solved yet. I close this issue.
Hello, I'm using the "Result library" on a spring boot kotlin project.
I faced to problems while I used some "Result" objects in a parameterized test with a method source annotated with
@JvmStatic
:ClassCastException
while accessing tovalue
attribute of myResult
object.Result
, and when I tried to usemyResult.isOk
, that returnedtrue
instead offalse
.My environment
Class cast exception problem
Here is an example of my code:
Note:
onSuccess
method calls thevalue
getter inside theResult
class.The error at runtime:
Note: I tried to use a non "jvmstatic" method, and I don't reproduce the error.
Example without the "jvmstatic" code :
Note: with the
@TestInstance(PER_CLASS)
, I faced to other issues so I don't want use this way.The error Result that has "isOk==true" problem
Always with this context, I tried to replace the
onSuccess
by aif
bloc that testsisOk
attribute.My unit test fails and when I glance at result state while debugging, I saw its
isOk
value returnstrue
when myResult
is an error.Code:
While debugging, on the line that contains
if (firstResult.isOk) {
, ifresult
is anErr(...)
,firstResult.isOk
givestrue
instead offalse
.My analyze
After looking the source code of class
Result
, I think the problem comes from theinlineValue
attribute that his type ofAny?
.The class cast exception occurs at this moment:
At compilation phase, I think some information is lost when I use a
Result<List<XXX>, ...>
but it only occurs when I use a method annotated@JvmStatic
. I don't face this issue with code that is not annonated@JvmStatic
.Maybe a reified value could solve the problem...?
Thanks in advance for your reply!