Skip to content
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

Kotlin Lambda fails parsing with Power-Mockito2 #965

Open
ebi-igweze opened this issue Dec 30, 2018 · 2 comments
Open

Kotlin Lambda fails parsing with Power-Mockito2 #965

ebi-igweze opened this issue Dec 30, 2018 · 2 comments

Comments

@ebi-igweze
Copy link

When running with PowerMockito 2, files with lambda expressions fail to parse and I get the following error

java.lang.IllegalStateException: Failed to transform class with name com.example.domain.CaseTest. Reason: [source error] ) is missing

// when you try something like this
val lambda = { it: String ->
    // do something funny
}

// or something like this with mockito
doAnswer { it:InvocationOnMock ->
    val arg1 = it.arguments[0]
    // do something funny
}.`when`(mockedObj).member(any())

It produces the error when you try using lambda expression like the ones defined above. I have to use
the following work around, what I had to do was create a member function for the test and pass it as a parameter. For instance

// define the function like so
private fun doSomething(it:InvocationOnMock) {
   val point = it.arguments[0]
   // do something funny
}


// then pass the function like so
whenever(defaultDisplay.getSize(any())).then(::doSomething)
@TWiStErRob
Copy link

@ebi-igweze this won't solve the problem, but may be a better workaround: try function-local functions, if they work you can at least keep those functions close to usage and maybe even access local variables.

@eljonny
Copy link

eljonny commented Apr 6, 2019

This is also an issue in Java 8.
Wrapping lambdas in getters works around the problem, but it looks to be at least related to this, if not actually being the same issue. I've had to work around this 3 or so times at work in the last 6 months, and it originally took me about 7 hours of debugging to figure out that this is actually the cause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants