jaredsburrows/android-gradle-java-app-template

Robolectric + PowerMock

Closed this issue · 10 comments

frel commented

Hi,

Thanks for all the work you've put into this. It's highly appreciated.

I'm trying to run tests that use Robolectric and PowerMock. But I'm getting the following issue when using the "PowerMockRule" method.

Caused by: java.lang.IllegalStateException: PowerMockRule can only be used with the system classloader but was loaded by org.robolectric.internal.bytecode.InstrumentingClassLoader@2f21a0e5

Do you have any ideas? I've tried setting the suggestions by robolectric.

If I use @RunWith(RobolectricGradleTestRunner.class) I cannot test static methods, which is what I need.

Thanks!

@frel Robolectric supports PowerMockito. Can you try that? See: https://github.com/robolectric/robolectric/wiki/Using-PowerMock.

May I ask why you are using PowerMock instead of Mockito?

frel commented

Thanks for replying. I have tried following that guide without any luck. I'm still getting the same issues.

As for Mockito I would prefer using PowerMock, because the project I'm refactoring is using EasyMock. Also the last time I looked up Mockito it didn't support mocking static, final and "expectNew", which PowerMock has.

EDIT: Forgive me, I misread PowerMock(ito). I will revisit that site.

@frel Ok. Let me know if you can get PowerMockito working.

frel commented

I am getting it to work in your project. But in my project with a lot of EasyMock tests I am getting these kinds of errors when running tests:

java.lang.IllegalArgumentException: class android.view.MotionEvent$$EnhancerByCGLIB$$5102438b is not an enhanced class

If I use PowerMockIgnore then it works, but I'm guessing mocking static etc would break. Any ideas?

I am not really sure. I try to stay away from mocking static/final classes/methods/fields.

@frel Any updates on this? Have you got it working?

frel commented

Hi @jaredsburrows, sorry for the late reply. In the end I gave up this. I was unable to get the power mockito and robolectric runner to work at the same time.

@frel I am sorry to hear that. If you do not have that many powermock tests, the quick switch to mockito will work great for you.

frel commented

I'm afraid there are too many tests, but I will consider using Mockito in future projects :) Thank you for all the help!

Anytime!