powermock

Mocking method calls using power mockito - org.powermock.api.mockito.ClassNotPreparedException

喜夏-厌秋 提交于 2019-12-04 22:33:07
I have an image loader class and i need to test some static methods in it. Since Mockito does not support static methods i switched to Power Mockito. But the static method i am testing has a method call Base64.encodeToString(byteArray, Base64.DEFAULT); To mock this i am using mockStatic method as below with @PrepareForTest annotation. PowerMockito.mockStatic(Base64.class); But Android studio is returning me still returning me an error as below. org.powermock.api.mockito.ClassNotPreparedException: The class android.util.Base64 not prepared for test. To prepare this class, add class to the '

Is it possible to mock a static method on a final class using a PowerMockRule instead of the PowerMockRunner?

女生的网名这么多〃 提交于 2019-12-04 20:59:33
问题 According to the PowerMock docs, I should be able to run using a PowerMockRule instead of @RunWith(PowerMockRunner.class) and get the same results. I seem to have found a case where this isn't true. The below sample runs fine: package com.test.powermockstatics; import static org.junit.Assert.assertEquals; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; import org.junit.Test; import org.junit.runner.RunWith; import org

Mockito throws NullPointer when creating a mock object

断了今生、忘了曾经 提交于 2019-12-04 18:39:17
问题 I have an integration test in which some set up is done using Guice. I'm using Mockito to mock some of the dependencies. This has worked fine for me until now. I needed to use PowerMock for some other dependency. Now Mockito is throwing a NullPointerException while loading one of its own classes: java.lang.ExceptionInInitializerError at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17) at org.mockito.exceptions.base

Missing jacoco.exec file when using jacoco offline instrumentation with Powermock

你说的曾经没有我的故事 提交于 2019-12-04 16:59:55
Despite apparently this post showed a solution to using powermock and jacoco, I haven't been able to make it work in a pretty simple project ( available on GitHub ). In my case, the test executes correctly but the jacoco.exec file is missing so jacoco doesn't check coverage. Test class: @RunWith(PowerMockRunner.class) @PrepareOnlyThisForTest(Util.class) @PowerMockIgnore("org.jacoco.agent.rt.*") public class UtilTest { @Test public void testSay() throws Exception { PowerMockito.mockStatic(Util.class); Mockito.when(Util.say(Mockito.anyString())).thenReturn("hello:mandy"); Assert.assertEquals(

Failing integration test for Apache Spark Streaming

醉酒当歌 提交于 2019-12-04 10:31:27
问题 I've been trying to track down an issue with some unit/integration tests I've been writing for an Apache Spark project. When using Spark 1.1.1 my test passed. When I tried to upgrade to 1.4.0 (also tried 1.4.1) the test starts failing. I've managed to reduce the code needed to reproduce the issue down to the small integration test below. Interestingly, if I comment out the @RunWith annotation on the test then the test passes correctly. Obviously I don't need the @RunWith annotation for this

Class loading collision between Robolectric and Powermock

别来无恙 提交于 2019-12-04 10:04:36
I'm trying to write a test that needs both Robolectric 2.2 and PowerMock , as the code under test depends on some Android libraries and third party libraries with final classes that I need to mock. Given that I'm forced to use the Robolectric test runner through: @RunWith(RobolectricTestRunner.class) ...I cannot use the PowerMock test runner, so I'm trying to go with the PowerMock java agent alternative, without luck so far. I have setup everything according to this guide but I'm facing a collision problem between classes required by the javaagent library and by robolectric through its

How do I verify the number of invocations of overloaded method using Mockito?

做~自己de王妃 提交于 2019-12-04 05:02:44
问题 How do I check if bar(Alpha, Baz) called bar(Xray, Baz) using Mockito - without actually calling the later, given my MCVE class Foo : public class Foo { public String bar(Xray xray, Baz baz) { return "Xray"; } public String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get() instanceof Xray) { return bar((Xray)alpha.get(), baz); } else if(alpha.get() instanceof Zulu) { return bar((Zulu)alpha.get(), baz); } else { return null; } } } Currently, I

Can I mock a superclass's constructor with Mockito/Powermock?

一笑奈何 提交于 2019-12-04 04:57:47
Is it possible using Mockito and optionally Powermock to mock a superclass S such that any calls to the superclass to S (including calls to the S() constructor) are mocked? So using the below example, if I replace S with MockS using Mockito, will the call to super() use the constructor in MockS ? class S { S() { // Format user's hard drive, call 911, and initiate self-destruct } } class T extends S { T() { super(); } } class Test { @Mock private S mockS; new T(); // T's call to super() should call the mock, not the destructive S. } I've seen questions about mocking individual methods in S or

NoClassDefFoundError for MockitoInvocationHandler class

一世执手 提交于 2019-12-04 04:30:48
I am using mockito-all-1.9.5-rc1.jar and powermock-mockito-1.4.12-full.jar . When I run this simple unit test for mocking final method in non-final class. import static org.junit.Assert.assertEquals; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) @PrepareForTest(ABC.class) public class ABCTest { @Test public void finalCouldBeMock() { final ABC abc = PowerMockito.mock(ABC.class);

InetAddress static method doesn't mock with PowerMockito

梦想的初衷 提交于 2019-12-04 04:29:24
问题 I am running into weird problem while trying to mock static methods in InetAddress. I am successfully able to mock static methods for many of other classes and all works fine, but InetAddress showing different behavior. I am using JUnit 4.x, Mockito 1.9.5 & PowerMock 1.5.6. Given below test using Mockito and PowerMock, and InetAddress mocking works fine - @RunWith(PowerMockRunner.class) @PrepareForTest({InetAddress.class}) public class UtilityTest { @Mock InetAddress inetAddress; @Test public