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
2
votes
1 answer

mock - creating new files (Java)

how to check if without creating a new directory? String st = "exemple"; String path = "exemple"; if (!new File(path).exists() && !new File(path).mkdirs()) { throw new ComumException("trocaarquivos.erro.exemple", path); } my…
guimas
  • 23
  • 4
2
votes
1 answer

Mock File.createTempFile() and return our mocked File object

How can we mock File.createTempFile() to return our mocked File object? It should return our mocked file object instead of creating a new temp file and return new object.
Knot
  • 141
  • 13
2
votes
1 answer

Why org.powermock:powermock-api-mockito:1.7.4 depends on two versions of mockito-core?

I am getting the following error during the run of Junit tests. java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:141) at…
Shashwat Kumar
  • 5,159
  • 2
  • 30
  • 66
2
votes
1 answer

Mockito cannot find defined class while running a test

I am having trouble with mockito dependencies.I wrote a sample test and when I ran it I got this exception java.lang.NoClassDefFoundError: javassist/NotFoundException at…
Tomas
  • 21
  • 1
2
votes
1 answer

UnfinishedStubbingException when working with doNothing method for void methods

The below code is causing UnfinishedStubbingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"),…
Coder17
  • 767
  • 4
  • 11
  • 30
2
votes
1 answer

How to fix java.lang.IllegalStateException: Cannot clear JavaAgentClassRegister. Set method has not been called.?

I am using JunitRunner for running unit tests written using PowerMock and Mockito . Spring Boot Version used is 2.0.5.RELEASE pom.xml has below dependencies …
Krish Gowda
  • 193
  • 2
  • 17
2
votes
0 answers

UnfinishedStubbingException when performing stubbing from a class other than the test class

I'm using PowerMock to mock a java.net.Inet4Address (amongst other things) to return a particular IP address (getHostAddress()), as well as whether or not it's loopback (isLoopbackAddress()). I find that if I perform the actual stubbing…
Tonxey
  • 51
  • 3
2
votes
1 answer

How to mock static methods with more than one argument

I'm writing a test case to test a component that invokes a static method accepting 5 arguments. I want to know how I can do that. Earlier I have been successful in mocking static method with 0 and 1 arguments. However when I mock a static method…
Singh
  • 23
  • 6
2
votes
2 answers

how to mock keystore.getInstance() which returns the keystore object?

I have tried to mock the KeyStore.getInstance() which returns the keyStore object here the below code which I had tired, Junit: KeyStore keyStoreMock = PowerMockito.mock(KeyStore.class);…
user12061625
  • 53
  • 1
  • 4
2
votes
1 answer

Testing method having CompletableFuture.allOf using mockito

I have a method called doParallelThings: public Dummy doParallelThings(Map mapp) throws Exception { Dummy dummy = new Dummy(); CompletableFuture one = firstService.getOne(mapp.get("some1"), mapp); …
Harshini
  • 459
  • 1
  • 7
  • 23
2
votes
1 answer

How to mock object that's created by a class builder?

In one of my running class currently i have: void run() { Food food = new FoodBuilder(apple, peach, grape) .build(); food.eatFood(); My FoodBuilder class is like this: public class FoodBuilder implements FoodInterface…
OPK
  • 4,120
  • 6
  • 36
  • 66
2
votes
0 answers

ClassCastException: io.realm.Realm cannot be cast to io.realm.Realm

I am trying to mock a Realm instance. My implementation has been successful, but it fails when running multiple tests together. Here is the implementation of a class to create the mocked Realm instance. import android.os.Build import…
Pedro Paulo Amorim
  • 1,838
  • 2
  • 27
  • 50
2
votes
4 answers

doNothing.When for private void method itself is invoking method

I'm trying to test one of my Class named Store and particularly a method named removeFromFavourite below is the method public void removeFromFavourite(Quote quote) { if(quote == null) { throw new…
Kumar Gaurav
  • 1,287
  • 3
  • 19
  • 47
2
votes
4 answers

Unable to use mocking to throw an exception - the thrown exception is not being caught

I have a method where I'd like to mock an exception being thrown so that the catch statement is entered: public static String func(String val) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); return…
John Bergqvist
  • 852
  • 13
  • 38
2
votes
1 answer

Using mock-maker-inline Mockito plugin on IBM JVM 1.8

I'm trying to mock a static method of a final class : Files.exist by example. I read that I have to user a mock-maker-inline mockito plugin so I set it up : I put this : mock-maker-inline in :…
Ceddoc
  • 243
  • 3
  • 14