Questions tagged [powermock]

Use this tag for questions about PowerMock, a Java library for creating mock objects for classes and methods. Questions about PowerMock's extension for Mockito should be tagged [powermockito] instead.

PowerMock is a Java unit testing library that takes mock creation further than is possible using Mockito and EasyMock and facilitates mock creation and behaviour in areas that they deem un-testable.

Facilities available from PowerMock include:

  • mocking static, private and final methods
  • partial mocking, and
  • mocking construction of new objects

The project is open source on Apache License 2.0 and is available in Git Hub.

2039 questions
0
votes
1 answer

How to mock tail recursive functions?

I want to test my code which has a few tail recursive functions. I couldn't mock the tail recursive functions because they need to declared either Final or Private. Most mocking frameworks don't support mocking such methods, the ones that support…
H Sridhar
  • 71
  • 7
0
votes
0 answers

How to Mock a method call made by a class initialised using new?

I am trying to write junits for a provisioning service. The service eventually makes a call to a VICE Client and gets a certificate, This wastes certificate when the junits are run in testing to I wanted to mock this http call to the outside…
Sameer
  • 757
  • 1
  • 14
  • 35
0
votes
3 answers

How to mock creation new object inside some service?

I have to test a method inside some class like this: public class aClassToTest{ private SomeService someService = new SomeService(); public String methodToTest(){ String data = someService.getData(); //.... } } So, I have mocked…
fonzy
  • 53
  • 1
  • 8
0
votes
0 answers

Integrating Powermock with kotlin for Instrumentation testing

I am trying to integrate Powermock with Kotlin to test date related logic using Calendar and shared preferences but i am always getting unresolved reference for Powermock but mockito object works fine. I have tried integrating all the powermock…
0
votes
1 answer

How can I mock a class which uses a SLF4J Log4J Logger?

I try to Mock a Class which instantiates a DatabaseConnection via Hibernate over an other Class, but I get errors of the SLF4J Error Factory in the Class, but I want the Logger to run in this Test to. How could I fix this to Mock the class and then…
Willey3x37
  • 310
  • 2
  • 12
0
votes
1 answer

NoClassDefFoundError: org/easymock/MockType

I am using PowerMock and EasyMock to mock system class like Math.class. But getting an error: java.lang.NoClassDefFoundError: org/easymock/MockType Class code - class ToBeTested { public int getAbsoulte(int a) { int res =…
Vikash Yadav
  • 713
  • 1
  • 9
  • 29
0
votes
1 answer

Writing an integration test for a service

I have a service class that I would like to perform the integration test (whitebox). The method code, @Async( ELLA_THREAD_POOL_EXECUTOR_NAME ) public void invokeEllaAsync( final IrisBo irisBo ) { if( isTrafficAllowed( irisBo ) ) { …
Arefe
  • 11,321
  • 18
  • 114
  • 168
0
votes
1 answer

EasyMock/PowerMock - Mocking Static Methods Throws Error: no last call on a mock available

I'm writing a simple Java Class to test EasyMock/PowerMock functionality to mock static methods from class. So I'm just mocking Math.random method to return a constant value for testing purpose. Here is my code: package x.y.z; import…
Vedant Terkar
  • 4,553
  • 8
  • 36
  • 62
0
votes
1 answer

Android -> EasyMock -> PowerMock -> BluetoothAdapter -> Failed resolution of: MocksControl$MockType

This app shows you can use createMock(BluetoothAdapter.class): https://github.com/riis/AndroidArduino.git So to try that, I add EasyMock and PowerMock to my build.gradle: androidTestImplementation 'org.powermock:powermock-core:1.6.5' …
Phlip
  • 5,253
  • 5
  • 32
  • 48
0
votes
1 answer

PowerMockito doesn't mock the new instance called in the tested class

I have the following class where, in the constructor, I call another constructor to build a field of the class: public class ClassIWantToTest { private ClassIWantToMock anotherClass; public ClassIWantToTest() { //some stuff …
Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
0
votes
2 answers

Mocking constructor using powermock on the class which needs to be tested

I am able to mock a constructor call using powermock from inside a class which I want to test. This works when I add the class I want to test in @PrepareForTest. But once I add my class there, even when the test cases pass, the coverage is being…
simba
  • 11
  • 4
0
votes
1 answer

Adding @RunWith(PowerMockrunner.class) to test gives all other tests NPE

I have a testclass where I am running Mockito tests, like: public class ViewModelTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); } @Test public void openQuestion() { …
CJR
  • 3,174
  • 6
  • 34
  • 78
0
votes
1 answer

Unit testing for a protected is failing

I have a class EllaService and I would like to test the EllaService::isTrafficIgnored method there. The code is provided below, @Service public class EllaService { @Autowired @Qualifier( ELLA_CONNECTOR_BEAN_NAME ) private…
Arefe
  • 11,321
  • 18
  • 114
  • 168
0
votes
1 answer

Wanted but not invoked - Error with Powermock while calling static method from another static method with predicate parameter

I am trying to test a utility class having static methods , most of them static and returning Optional. Lot of other objects and parameter are being passed as parameters which i have mocked using Mockito. I am using PowerMock to call static…
JavaGen
  • 1
  • 1
0
votes
1 answer

java.lang.NullPointerException on Url mock tests

My test for Url connection worked correctly. Then after 1 month I ran all tests in the app and test for URL connection failed, I don't know why as I didn't change this class. Please, could someone look at this test? The test now throws a…
Bartosz Kolej
  • 97
  • 1
  • 12
1 2 3
99
100