Open GoogleCodeExporter opened 9 years ago
Additional StackOverflow question: http://stackoverflow.com/q/20557769/16487
Original comment by C.Ross.E...@gmail.com
on 17 Dec 2013 at 2:38
Thanks, please report back here if you find any work-arounds.
Original comment by johan.ha...@gmail.com
on 18 Dec 2013 at 3:41
I can get the rule mode to to work using the following code, but I then get
IllegalAccessError whenever MainActivity or TestMainActivity tries to access an
anonymous inner class (i.e. MainActivity$1 or TestMainActivity$1). I have to
refactor the code to avoid anonymous inner classes to get it to run.
@PrepareForTest(fullyQualifiedNames = { "application.package.MainActivity" })
@PowerMockIgnore({ "*" })
@RunWith(RobolectricTestRunner.class)
public class TestMainActivity {
@Rule
public PowerMockRule rule = new PowerMockRule();
Original comment by goo...@sa.me.uk
on 5 Jun 2014 at 7:03
I can't easily find a more restrictive @PowerMockIgnore() list. It needs to
include org.mockito.* and org.powermock.* but it starts breaking static fields
in Robolectric (specifically, org.robolectric.Robolectric.application is null
during the test run).
When org.robolectric.* is excluded I get a LinkageError:
java.lang.LinkageError: loader constraint violation: when resolving field
"application" the class loader (instance of
org/powermock/core/classloader/MockClassLoader) of the referring class,
org/robolectric/Robolectric, and the class loader (instance of
org/robolectric/bytecode/AsmInstrumentingClassLoader) for the field's resolved
type, bytecode/AsmInstrumentingClassLoader, have different Class objects for
that type
If I specifically include the $1, $2, etc. anonymous classes in @PrepareForTest
then I can use them in the test without an IllegalAccessError.
Original comment by goo...@sa.me.uk
on 5 Jun 2014 at 6:40
The following works and I don't have any issues with anonymous classes:
@RunWith(RobolectricTestRunner.class)
@PrepareForTest(fullyQualifiedNames = { "application.package.MainActivity" })
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
public class TestMainActivity {
@Rule
public PowerMockRule rule = new PowerMockRule();
The "org.mockito.*" exclusion is necessary or it fails to initialise.
The "org.robolectric.*" exclusion is necessary or
org.robolectric.Robolectric.application will be null.
the "android.*" exclusion is necessary or there will be two different instances
of the android.app.Application class present causing an error for the
org.robolectric.Robolectric.application field.
Original comment by goo...@sa.me.uk
on 6 Jun 2014 at 5:50
I tried implementing the above, but I keep running into ClassCastExceptions
where it cannot cast between Class A and Class A, but with the classloaders as
org.powermock.core.classloader.MockClassLoader and
org.robolectric.bytecode.AsmInstrumentingClassLoader.
I only have two classes listed in my @PrepareForTest, but the PowerMock
classloader seems to be loading everything in the class under test.
Original comment by C.Ross.E...@gmail.com
on 9 Jun 2014 at 12:42
Class B is referencing Class A and one of them is excluded, you need to exclude
the other one with @PowerMockIgnore too. I'm using the rule+xstream method not
the agent (which fails to work at all because it rejects the system class
loader).
Original comment by goo...@sa.me.uk
on 9 Jun 2014 at 12:59
Original issue reported on code.google.com by
davidst@gmail.com
on 15 Nov 2013 at 10:13