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

strange issue with throws block #36

Closed
stalexxx opened this Issue Mar 5, 2018 · 10 comments

Comments

2 participants
@stalexxx

stalexxx commented Mar 5, 2018

Here https://github.com/stalexxx/ad-scrapper/blob/3f38e75d806ad9b3ea99b019280728fe78cd29d0/avito/src/test/kotlin/com/stalex/avito/RetryTest.kt I firstly made test "test always bad with mockk", but it failed stangly, although it should do the same as "test always bad"(that works):
in debugger it goes until line 35, but then fails with exception on line 33

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

Let me check what is wrong

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

Can you paste an exception message with stacktrace

@oleksiyp oleksiyp added the help wanted label Mar 5, 2018

@stalexxx

This comment has been minimized.

stalexxx commented Mar 5, 2018

well, i got that
java.lang.IllegalStateException
at com.stalex.avito.RetryTest$3.invoke(RetryTest.kt:33)
at com.stalex.avito.RetryTest$3.invoke(RetryTest.kt:10)
at io.kotlintest.Spec$runTest$callable$1$1.invoke(Spec.kt:124)
at io.kotlintest.Spec$runTest$callable$1$1.invoke(Spec.kt:15)
at io.kotlintest.Spec$runTest$initialInterceptor$1$1.invoke(Spec.kt:116)
at io.kotlintest.Spec$runTest$initialInterceptor$1$1.invoke(Spec.kt:15)
at io.kotlintest.Spec.interceptTestCase(Spec.kt:78)
at io.kotlintest.Spec$runTest$initialInterceptor$1.invoke(Spec.kt:116)
at io.kotlintest.Spec$runTest$initialInterceptor$1.invoke(Spec.kt:15)
at io.kotlintest.Spec$runTest$callable$1.call(Spec.kt:124)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

but you can run it yourself (https://github.com/stalexxx/ad-scrapper, RetryTest.kt)

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

Very weird error. Not able to understand how that's possible

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

Ok, now understand

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

It's about how Kotlin and MockK interact regarding Boxed numbers

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

As a workaround you can use Parser<String, Int?>. It should pass. Meanwhile I get closer on what can be done with non-nullable integer

        "test always bad with mockk" {
            val parser = mockk<Parser<String, Int?>>()
            every { parser.parse(any()) } throws IllegalStateException()

            val result = RetryableParser(parser, default = { -1 }).parse("")

            result shouldBe -1
        }.config(enabled = true)
@stalexxx

This comment has been minimized.

stalexxx commented Mar 5, 2018

well, Int is some random type, doesn't matter, but thank you, my tests are green )

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 5, 2018

Are you sure it doesn't matter?

Because what I understood Kotlin is returning Int object from Parse and then any first possible time converts it to int via intValue() , but because you pass it from mock to other type of Parser it keep the object Int and only when it assigned to val calls intValue() which results in exception(because it is mocked). That way intValue() get out of try {} catch(...) {} and throws Excecption

Mocking works like chain of calls here every { parser.parse(any()).intValue() } throws IllegalStateException() (because Kotlin is implicitly inserting intValue())

@oleksiyp oleksiyp added the bug label Mar 5, 2018

oleksiyp added a commit that referenced this issue Mar 7, 2018

oleksiyp added a commit that referenced this issue Mar 8, 2018

@oleksiyp

This comment has been minimized.

Collaborator

oleksiyp commented Mar 8, 2018

Version 1.7.10 released, please check if it fixes your case and close the ticket.

@oleksiyp oleksiyp closed this Mar 13, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment