Questions tagged [powermockito]

Use this tag for questions about PowerMockito, a Java Framework that allows the mocking of usually un-mockable types, i.e. statics, and private methods, in conjunction with the Mockito framework. Questions about using PowerMock with EasyMock should be tagged [powermock] instead.

PowerMockito is used in addition with the Java Framework Mockito. PowerMockito's main strength is the ability to mock Static and Private Methods. A good example of PowerMocktio is the ability to mock Singletons which are generally difficult when unit testing.

Heres a great link to starting out with PowerMockito: Using PowerMock with Mockito

1326 questions
6
votes
1 answer

Any way to mock Files.write(...) method?

I need help with the unit test case. I want to mock static method write(Path path, byte[] bytes, OpenOption... options) of java.nio.file.Files class. I tried eg. in that way: PowerMockito.doReturn(path).when(Files.class, "write", path,…
6
votes
1 answer

PowerMock(with mockito) simple unit test error

I am using Mockito + PowerMock to write a simple unit test for the following singleton class: public class MyService { private static MyService service; private List schoolList; private MyService(){ // test case error…
user842225
  • 5,445
  • 15
  • 69
  • 119
6
votes
2 answers

How do I set a primitive private static final field in a class with private constructor using PowerMockito?

I'm trying to set a private static final field in a class with private constructor for a JUnit test. When I boil the code down to its basics, I get the following: public class Foo { private static final boolean FLAG = false; private Foo() {…
Christian
  • 6,070
  • 11
  • 53
  • 103
6
votes
3 answers

issue while using powermockito to mock the URL class

Without going into the details of the merit of doing this way, just need help figuring out why the following test codes do not work! This has been more of a learning exercise at this point.. Just trying to use PowerMockito to create a mock for the…
apil.tamang
  • 2,545
  • 7
  • 29
  • 40
6
votes
1 answer

PowerMock java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

I created a mock HttpsURLConnection based on an StackExchange answer: import java.net.URL; import javax.net.ssl.HttpsURLConnection; ... @RunWith(PowerMockRunner.class) public class DialogTest { public void mockHttpsUrlConnectionExample()…
Donal Lafferty
  • 5,807
  • 7
  • 43
  • 60
5
votes
1 answer

Is there any way to mock a sealed interface?

Today I created a small test project for java 16 preview. I created a sealed interface and a final implementation. I want to know if there is anyway to be able to mock the sealed interface so that I can test implementations that use the sealed…
Curious
  • 51
  • 1
  • 6
5
votes
0 answers

org.objenesis.ObjenesisException while running powermockito with JDK 8

I am trying to mimick file reading in my unit test case using powermockito, below is my code I want test import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class FileOperation { …
LowCool
  • 1,187
  • 5
  • 25
  • 51
5
votes
1 answer

The type org.powermock.api.support.membermodification.MemberModifier cannot be resolved. It is indirectly referenced from required .class files

I have following jars in my class path powermock-module-junit4-2.0.7.jar powermock-api-mockito2-2.0.7.jar powermock-api-support-1.4.9.jar mockito-core-2.8.9.jar junit-4.10.jar I have imported following classes. import static…
Tech Geek
  • 437
  • 1
  • 4
  • 19
5
votes
1 answer

How to mock kotlin object for unit testing

I have a sample method(which I need to write test case) as given below, fun setName(val auxName:String) { val configUrl = getConfig(auxName) } I want to mock the getConfig method and return a specific string value. getConfig is a…
Amrutha Jaya Raj
  • 612
  • 2
  • 10
  • 32
5
votes
0 answers

Sporadic behavior No argument value was captured! ArgumentCaptor for Static NewRelic using PowerMockito SpringBoot

Adding NewRelic custom parameters to critical part of logging library. Hey I should add tests right to verify called correctly. wrote several tests to verify and ArgumentCaptor for NewRelic(), sometimes they work. Sometimes, they don't. Using…
ken
  • 666
  • 2
  • 6
  • 19
5
votes
2 answers

Error trying to mock constructor for ProcessBuilder using PowerMockito

I am trying to mock the constructor for ProcessBuilder. The problem is that when the constructor is called it return null. Class code: public static void enable() throws IOException, InterruptedException { logger.info("Enable NTP…
Sebi95
  • 161
  • 4
  • 13
5
votes
4 answers

Powermockito java.lang.VerifyError

I am using powermock.mockstatic this line blow so that I can control its returning value since its only swing I dont have to test it. @Before public void setUp() throws Exception RelatedIntelligencePanel rel =…
MertG
  • 393
  • 1
  • 4
  • 15
5
votes
2 answers

How to undo/reset PowerMockito.mockStatic?

Assuming I have the following classes public class StaticClass { public static void staticMethod() throws SomeException { System.out.println("staticMethod"); } private StaticClass() { } } and public class SomeClass { …
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
5
votes
1 answer

Powermockito private method mock NullPointerException. Calls the private method

I'm trying to mock a private method (executeGetRequest) and in the line that I'm declaring the mock that I want returned for the private method, the private method is actually being executed with null arguments, therefore throwing a…
nbrest
  • 137
  • 2
  • 11
5
votes
3 answers

Jacoco is not showing coverage for PowerMockito classes

We are using Jacoco and eclemma for test case coverage. For the classes that are not using PowerMockRunner we are getting coverage properly in both.For the classes that uses PowerMockRunner we are facing issue with coverage like it shows 0 %…
Pradeep
  • 1,947
  • 3
  • 22
  • 45