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
3
votes
0 answers

org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class

I know this has been asked before but my case seems quite unique, I just upgraded to JDK 11 on one of my android projects, as a result, had to update Mockito and PowermMckito as well. Since then I'm facing this problem. Argument should be a mock,…
3
votes
0 answers

PowerMock @PrepareForTest throws exception for a class containing a sorted call [JAVA 8]

I have a class(TempClass) containing a map that uses a static method of another class(UtilClass) to generate a list using this map. For mocking the static method(can't avoid using the static method), I'm using Powermockito. However, I'm getting this…
Beginner
  • 61
  • 11
3
votes
2 answers

Using Mockito inline mockConstruction the same as PowerMocito whenNew

I am trying to update the existing code to remove PowerMockito and replace it with Mockito now that Mockito supports mocking static and mocking construction. I get that you can do the follow: assertEquals("foo", new Foo().method()); try…
WolverinEli
  • 59
  • 1
  • 5
3
votes
3 answers

Unable to Mock System.currentTimeMillis()

I am writing unit tests using TestNG. The problem is when I mock System.currentTimeMillis, it returns the actual value instead of the mocked one. Ideally, it should return 0L , but it returns the actual value. What should I do to proceed? class…
Dawson Smith
  • 473
  • 1
  • 6
  • 15
3
votes
2 answers

What is the argument matcher for a Mono type parameter?

Class under test: import java.time.Duration; import java.time.Instant; import java.util.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import…
mantri
  • 3,031
  • 4
  • 16
  • 19
3
votes
1 answer

Powermock of static class gives error: java.lang.NoClassDefFoundError: Could not initialize class XXX

Problem Could not initialize class ... ...javax.xml.transform.FactoryFinder (in our case). In the article, where we found the solution, it was the class SessionFactory. Class Under Test We wanted to write a test for a utils class with static…
Mue
  • 191
  • 2
  • 8
3
votes
1 answer

PowerMockito calls real method on Mocked Object when defining second "when" clause

I am trying to define some different mocked behaviours when a method is called with different parameters. Unfortunately, I find that the second time I try to mock the given method on a (mocked) class, it runs the actual method, causing an exception…
Borzi
  • 537
  • 1
  • 5
  • 21
3
votes
1 answer

tried to access class javax.crypto.JceSecurity from class javax.crypto.Cipher JUNIT

I am trying to mock the cipher.doFinal() in a method which actually doing an AES encryption, using JUNIT. I am getting the exception while starting the JUNIT test case "tried to access class javax.crypto.JceSecurity from class…
User_1940878
  • 321
  • 1
  • 6
  • 25
3
votes
1 answer

Getting error java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock

When i am trying to create junit for controller i am getting below error: java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock my pom.xml…
Deepak Malav
  • 306
  • 1
  • 5
  • 13
3
votes
1 answer

How to PowerMockito.doNothing when a static method of a final class is called?

I have this line of code in one of the utility class System.setProperty("someProperty", ); I want nothing to be happened, when the above line of code is executed through the tests. I have annotated the class with following…
Neha
  • 31
  • 2
3
votes
1 answer

Mocking private method call using PowerMockito returns null instead of returning List: (Want not to execute private method)

I am trying to unit test a method using JUnit4. The method under test is calling another private method and I would like to mock it using PowerMockito. My method is like below: Class MyClass { public List myMethod(String name) throws…
3
votes
3 answers

DynamoDB put Item is not getting mocked using MockiTo & PowerMock

Below is the code which I am trying to Mock, this.dynamoDb.getTable(PropertyUtil.get(Constants.SOME_TABLE_NAME)) .putItem( new PutItemSpec() .withItem(new Item().withString("ID", pId).withString("eId",…
ViS
  • 1,357
  • 1
  • 17
  • 36
3
votes
3 answers

Mocking file reading/writing via JUnit

How do you mock file reading/writing via JUnit? Here is my scenario MyHandler.java public abstract class MyHandler { private String path = //..path/to/file/here public synchronized void writeToFile(String infoText) { // Some…
lemoncodes
  • 2,371
  • 11
  • 40
  • 66
3
votes
1 answer

How to mock BitmapFactory: Method decodeFile

I have a program which creates thumbnail image from a full size image saved in the storage area . I am trying to test that functionality using mockito but it gives me the following error: java.lang.RuntimeException: Method decodeFile in…
Paramita
  • 83
  • 2
  • 11
3
votes
1 answer

Junit 4.x + Hibernate 5.x with H2 database

I am not using any framework just using maven war module and want to test the DAO layer using Juit 4 + Powermockito (first time). My idea is when I call CustomerDao to test createCustomer. First statement of this method is as below: Session session…
arvind kaushik
  • 141
  • 2
  • 6