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

PowerMockito using InjectMocks error

I have a very complicated class to write Junit test case. I decided to use PowerMockito since my class for which the test is to be run has a constructor initialization. My main class is like this: public class MainClass extends…
v1shnu
  • 2,211
  • 8
  • 39
  • 68
3
votes
2 answers

How to mock Hibernate Query.list() using Mockito

This is my class which need to be tested: @Repository @Transactional public class ProductDAOImpl implements ProductDAO { private static final Logger logger = Logger.getLogger(ProductDAOImpl.class); @Autowired private SessionFactory…
Sumit
  • 856
  • 5
  • 18
  • 38
3
votes
3 answers

Mocking spring method annotation throws NullPointerException in test cases

I have a custom validation annotation in a spring project. The aim for annotation is to accept the parameter names that are supposed to be id and dept values. The aim of the aspect is to fetch the paramNames from the annotation, find the…
Vijay Kalidindi
  • 165
  • 3
  • 12
3
votes
2 answers

Cannot mock/spy final class using PowerMockito.spy()

I'm trying to use PowerMockito to create a spy of a final class but I keep getting the following error, even though I am using PowerMockito's spy() method in place of Mockito's: java.lang.IllegalArgumentException: Cannot subclass final class class…
spaaarky21
  • 6,524
  • 7
  • 52
  • 65
3
votes
0 answers

PowerMockito mock a static method only in one testcase

I need to mock a static method only in one test case and wants it to return the original value in all the other testcases. Class ClassWithStaticMethod { public static boolean aStaticMethod(String param) { //Some Code; return…
3
votes
1 answer

powermock error java.lang.LinkageError: loader constraint violation: when resolving method "android.support.v4.app

I am using powermock to test and Android activity @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({ "org.mockito.*", "org.robolectric.*",…
drlobo
  • 2,139
  • 5
  • 32
  • 42
3
votes
1 answer

Mocking cassandra session object

I an trying to mock the session object of cassandra which is obtained in the actual code in the following way... session = cluster.connect(keyspace); What I am looking for is "To execute the statement and return the mock session object" I have tried…
Ram
  • 327
  • 1
  • 4
  • 18
3
votes
0 answers

Android Espresso Test: java.lang.TypeNotPresentException: Type org/powermock/modules/junit4/PowerMockRunner not present

I'm trying to use Powermock to mock static methods on Android Espresso Tests Cases, but I'm receiving this error. My tests are running fine with AndroidJUnit4.class Runner and I am able compile them with the PowerMockRunner, this error happens in…
3
votes
1 answer

How to test the number of times a static method in Java is called?

I have a class with methodA() that eventually calls a static methodB() that returns a String. methodA() can cause multiple invocations of methodB(), depending on the scenario. I want to write a unit test to verify that a single invocation of…
user1175969
  • 540
  • 6
  • 19
3
votes
1 answer

How to mock current date?

I have a method in my code which verifies that current day is business. it checks database calendar for this. method looks like this: public boolean iBusinessDayToday() { LocalDate today = LocalDate.now(); //logic with today } I need to…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
3
votes
3 answers

NullPointerException at constructor definition

I am trying to test the following class: public final class ClassA { private final ClassB member; public ClassA() { this.member = new ClassB(); } } The following is the test…
alpha_ulrich
  • 536
  • 5
  • 21
3
votes
1 answer

Can not mock the private method with mockito

I am trying to mock a private method with power mockito, after reading this post I got some idea and I followed the same structure: example here is my class: public class test(){ private long…
HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
3
votes
1 answer

How to mock a protected static inner class with PowerMockito

I have a public outer class with a protected static inner class that I need to mock out for unit testing. We are using Mockito and PowerMockito but I have not been able to find anything in this vein during my searches. Does anyone have any ideas?…
3
votes
0 answers

Not able to return value when using the following methods Mockito.when(), PowerMockito.when(),Mockito.doReturn()

Following are my classes I have a interface with the following method. The interface is as follows and has one of the method public interface SimpleDocumentManager{ public List getUserDocIdOfRelatedDocumentsForTemplate(String docType,…
Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44
3
votes
1 answer

Error while Mocking static function call

I am trying to Mock static but getting error message as below org.mockito.exceptions.misusing.MissingMethodInvocationException: when() requires an argument which has to be 'a method call on a mock'. For example: …
Sandeep
  • 233
  • 4
  • 13