PowerMock broken with Robolectric 3.1+ #2208

Open
karlicoss opened this Issue Jan 13, 2016 · 26 comments

Projects

None yet
@karlicoss
Contributor

Turns out 3.0 worked fine, so it broke quite recently. I've created a simple demo project: https://github.com/karlicoss/RobolectricPowermock

It fails with the following exception:

com.thoughtworks.xstream.converters.ConversionException: Cannot convert type org.apache.tools.ant.Project to type org.apache.tools.ant.Project
---- Debugging information ----
class               : org.apache.tools.ant.Project$1
required-type       : org.apache.tools.ant.Project$1
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/this$1/outer-class/dependencyResolver/dependencyResolver/project/isLoggingMessage/outer-class
line number         : 198
class[1]            : org.apache.tools.ant.Project
class[2]            : org.robolectric.internal.dependency.MavenDependencyResolver
class[3]            : org.robolectric.internal.dependency.CachedDependencyResolver
class[4]            : org.robolectric.RobolectricGradleTestRunner
class[5]            : org.robolectric.RobolectricTestRunner$HelperTestRunner
class[6]            : org.robolectric.RobolectricTestRunner$HelperTestRunner$1
class[7]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[8]            : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : not available
-------------------------------
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:439)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:480)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:412)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:66)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:50)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:134)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:32)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1206)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1190)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1061)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1052)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:255)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:151)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
@karlicoss
Contributor

Ok so I think I bisected the commit responsible for this regression: c4d5d29 (PR #2039)

@karlicoss
Contributor

Apparently, this is somehow related #2153

@hzsweers

@erd @jongerrish is anyone looking into this? Would be a dealbreaker for a lot of people considering how many static classes android has (Compat classes, BuildConfig, etc)

@jongerrish
Contributor

@hzsweers I think we should make Robolectric support PowerMock. For code outside the Android framework certainly. For code inside the Framework using PowerMock shouldn't be necessary if there is adequate shadow support. Can you give some examples of when you'd like to use PowerMock on the Android framework?

My feeling is that if possible refactor your own code to use DI and Mockito where possible. In cases where this is not possible use PowerMock. For Framework code, Robolectric shadows.

I don't know if anyone is working on this, I guess it needs someone to step up and volunteer ;-)

@karlicoss
Contributor

@jongerrish I tried fixing it, but apparently it didn't work. Although, there are couple questions about Robolectric internals in the PR comments #2213, so if you could elaborate on them, would be great, it might help me to figure out what exactly broke it.

Also, look at this test https://github.com/robolectric/robolectric/pull/2213/files#diff-ca1cf49ca80a766d69778b93b3d16db9R1. It doesn't try to invoke Powermock on Robolectric-instrumented classes, so it looks broken for all classes if your tests are under Robolectric runner.

@karlicoss
Contributor

@hzsweers I guess I finally fixed that :D #2390
@jongerrish could you please take a look?

@marklapasa

+1 Please fix

@marklapasa

@jongerrish: "For code inside the Framework using PowerMock shouldn't be necessary if there is adequate shadow support."

I am trying to use PowerMock/Robolectric with java.security.KeyStore. There has been no support for KeyStore for a long time now.

@jongerrish
Contributor

Correct. You should not need to mock, regular Mockito or power mock
framework methods. If there is missing support for keystore we'd welcome a
pull request to add it :-)

On Jul 28, 2016 2:16 PM, "Mark Lapasa" notifications@github.com wrote:

@jongerrish https://github.com/jongerrish: "For code inside the
Framework using PowerMock shouldn't be necessary if there is adequate
shadow support."

I am trying to use PowerMock/Robolectric with java.security.KeyStore.
There has been no support for KeyStore for a long time now.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2208 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AGBaeFD0dcFcIdbrHuOSe92lhfre5zNyks5qaRwXgaJpZM4HEMEQ
.

@Russel-Zhang
Russel-Zhang commented Aug 11, 2016 edited

@marklapasa
Have you tried to ingnore the "javax.crypto.* package"?
`@RunWith(RobolectricTestRunner.class)

@PowerMockIgnore({ "org.mockito.", "org.robolectric.", "android.", "javax.crypto." })

@PrepareForTest(YourStatic.class)
public class MyFoo {

}`

@dhirenmudgil

Please fix. As i am using clover sdk and there is a static class to this which i need to mock.

@liupengking1

+1 Please fix

@poldz123

+1

@ending0421

+1

@jamesneville

+1 to fix this, please.

@CoderSpinoza

+1 to fix this!

@clement-leprovost clement-leprovost added a commit to algolia/algoliasearch-client-android that referenced this issue Oct 3, 2016
@clement-leprovost clement-leprovost [test] Remove `PowerMockTestCase`
It is causing weird errors similar to [this one](robolectric/robolectric#2208):

```
Cannot convert type org.apache.tools.ant.Project to type org.apache.tools.ant.Project
```

And it doesn’t seem to serve a real purpose—at least not any longer.
eb7c982
@YorkShen

+1 to fix this, pls

@epool
epool commented Oct 26, 2016

I still getting a similar error on 3.1.3:

com.thoughtworks.xstream.converters.ConversionException: Failed calling method
---- Debugging information ----
message             : Failed calling method
cause-exception     : com.thoughtworks.xstream.converters.ConversionException
cause-message       : Failed calling method
method              : org.apache.tools.ant.Project$AntRefTable.writeObject()
-------------------------------

    at com.thoughtworks.xstream.core.util.SerializationMembers.callWriteObject(SerializationMembers.java:151)
    at com.thoughtworks.xstream.converters.reflection.SerializableConverter.doMarshal(SerializableConverter.java:257)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:90)
    at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
    at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
...
@daniellAlgar

Same here on 3.1.4

com.thoughtworks.xstream.converters.ConversionException: Cannot convert type org.apache.tools.ant.Project to type org.apache.tools.ant.Project
---- Debugging information ----
class               : org.apache.tools.ant.Project$1
required-type       : org.apache.tools.ant.Project$1
converter-type      : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /org.powermock.modules.junit4.rule.PowerMockStatement$1/outer-class/fNext/next/this$1/outer-class/dependencyResolver/dependencyResolver/project/isLoggingMessage/outer-class
line number         : 331
class[1]            : org.apache.tools.ant.Project
class[2]            : org.robolectric.internal.dependency.MavenDependencyResolver
class[3]            : org.robolectric.internal.dependency.CachedDependencyResolver
class[4]            : org.robolectric.RobolectricTestRunner
class[5]            : org.robolectric.RobolectricTestRunner$HelperTestRunner
class[6]            : org.robolectric.RobolectricTestRunner$HelperTestRunner$1
class[7]            : org.junit.internal.runners.statements.RunBefores
class[8]            : org.powermock.modules.junit4.rule.PowerMockStatement
class[9]            : org.powermock.modules.junit4.rule.PowerMockStatement$1
version             : not available
-------------------------------

at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:439)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:263)
at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:65)
...
@bartoszwilk

+1

@itanoss
itanoss commented Nov 24, 2016

+1

@kmenager

I've got the same issue with Realm and Unit test.
I just change the @PowerMockIgnore
from
@PowerMockIgnore({"org.mockito.", "org.robolectric.", "android."})
to
@PowerMockIgnore({"org.mockito.
", "android.*"})

And now, I can run my Realm static test with powermock
Hope this help pending a new release.

@abhikmitra
abhikmitra commented Dec 20, 2016 edited

+1

@xian
Member
xian commented Jan 5, 2017 edited

Deduping: #2677 [actually not a dupe].

@xian xian closed this Jan 5, 2017
@xian xian reopened this Jan 6, 2017
@xian xian changed the title from Robolectric 3.1-SNAPSHOT doesn't work in conjunction with PowerMock to PowerMock broken with Robolectric 3.1+ Jan 6, 2017
@xian xian added the defect label Jan 6, 2017
@xian xian added this to the 3.2.2 milestone Jan 6, 2017
@xian xian self-assigned this Jan 10, 2017
@xian xian modified the milestone: 3.2.2, 3.3.1, 3.3 Jan 11, 2017
@daidai513260

I'm not sure if this issue get fixed, but I still got the same error message under org.robolectric:robolectric:3.2.2

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