https://kotlinlang.org logo
#android
Title
# android
b

brainail

04/25/2018, 10:23 AM
Hi everyone 🙂 Was anybody able to use PowerMock + Kotlin + plain junit4 + backticks for all methods? For the first run everything was great but then nothing works again, javassist sometimes logs strange errors about bytecode or methods merely can’t be found by JUnit4TestRunnerXXX.., or Android Studio and even gradle task via console aren’t able to find any tests even if some test should be failed. I’m interested in backticks usage. It seems like transformation of such method to Java bytecode + bytecode manipulations of PowerMock break everything… I know that there is
test
channel but I think it’s can be more general problem. When for instance I have more than 10 tests written with backticks then nothing works (via gradle too) with the following error:
Copy code
java.lang.IllegalStateException: Failed to transform class with name com.X.X.X.SenderTest. Reason: [source error] ) is missing

	at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:284)
	at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:192)
	at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass1(DeferSupportingClassLoader.java:77)
	at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:67)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:161)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:48)
	at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:108)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:71)

Caused by: compile error: ) is missing
	at javassist.compiler.Parser.parsePrimaryExpr(Parser.java:1250)
	at javassist.compiler.Parser.parsePostfix(Parser.java:1031)
	at javassist.compiler.Parser.parseCast(Parser.java:921)
	at javassist.compiler.Parser.parseUnaryExpr(Parser.java:886)
g

gildor

04/25/2018, 10:28 AM
hmmm, looks like some problem of PowerMock
b

brainail

04/25/2018, 10:30 AM
So, it means that after Kotlin transformations with code (like applying underscore instead of spaces) PowerMock works with some invalid version of it..
g

gildor

04/25/2018, 10:30 AM
applying underscore instead of spaces
Kotlin doesn’t do that!
b

brainail

04/25/2018, 10:31 AM
Isn’t it kotlin compiler thing?
g

gildor

04/25/2018, 10:31 AM
no
Kotlin do not use any JVM members name normalization
only names supported by JVM
for example space allowed to do by JVM, but not by Java
You can easily check it if take a look on Kotlin bytecode (but not Java code decompiled from bytecode!)
b

brainail

04/25/2018, 10:34 AM
Oh I see now, misunderstood that thing. I didn’t know that JVM allows it directly and just skip some prohibited characters.
Anyway looks like for some reasons backticks in Kotlin test break everything. Without them it works almost fine.
g

gildor

04/25/2018, 10:35 AM
For example Scala uses normalisation and allows to use characters not available for names on JVM, but Kotlin doesn’t
👍 1
As I said, maybe some bug of Power Mock
Failed to transform class with name
b

brainail

04/25/2018, 10:41 AM
I gave up on it… Wasted too much time. Will try your suggestion with mockk.
g

gildor

04/25/2018, 10:46 AM
To be honest PowerMock has really bad reputation because of such problems, and of course because of it’s main feature %)
But of course, cannot be sure, maybe this crash of PM is just symptom of some other problem
b

brainail

04/25/2018, 11:05 AM
Yes of course PowerMock it’s a bad sign but there was legacy code which should be covered by tests without touching it…
@gildor only one tool is missed in API is that it’s not possible to mock object creation like
whenNew
in PowerMock and it should be wrapped via some creation factory in each case. Also I wonder does it use the same javasist under the hood or it’s something better and not so risky? 🙂
g

gildor

04/25/2018, 1:52 PM
Not sure about implementation details, but author of mockk @oleksiyp in this slack, maybe he can answer
o

oleksiyp

04/25/2018, 2:07 PM
Under the hood it is byte buddy for mockito, mockk and power mock. From point of view of stability it solely depends on mocking framework. What cases are handled. Mockk still strugling with spy and callOriginal things. I am going to rethink things and reimplement it. Main difference beetween frameworks is that I am focusing on Kotlin features and in mockito and powermock this is not first goal.
👍 1
b

brainail

04/25/2018, 4:01 PM
@oleksiyp So, then it still means that the error above will be possible due to javasist from PowerMock if you rely on it, right?
o

oleksiyp

04/25/2018, 4:28 PM
I thought PM migrated to byte buddy, but seems it is not. Try updating PM to latest if possble.
b

brainail

04/26/2018, 5:23 AM
Tried beta, 2.0, dependencies still contain Javasist and byte buddy added yes, but no luck :(