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
4
votes
4 answers

Not able to fully mock RestHighLevelClient

I need to mock RestHighLevelClient to test my code. Basically when i call "search" method of RestHighLevelClient i get either UnfinishedStubbingException or…
Taras Fityo
  • 77
  • 1
  • 10
4
votes
3 answers

PowerMockito mock private methods in springboot

I am trying to mock a private method inside my class under test which is as below. public String processPayment(...) { //some lines privateMethod(...); return ""; } private Object privateMethod(...) { //some lines return…
Praveen Kamath
  • 959
  • 2
  • 10
  • 18
4
votes
2 answers

how to mock UUID?

is it possible to mock UUID? or any problem in my Source Code? Look at exmaple: MyTest Class @RunWith(PowerMockRunner.class) @PrepareForTest({UUID.class,ActualClass.class,MyTest.class}) public class MyTest extends AbstractMuleContextTestCase{ …
karthick S
  • 51
  • 1
  • 2
  • 9
4
votes
1 answer

Can't mock java.lang.System#exit(int) method with PowerMock

My application has a flow that in the end of it the method System.exit(int) is being called. I'm trying to test this flow by running a test using TestNG. However, when running the test I'm getting this weird message although the test was…
user3364652
  • 480
  • 8
  • 23
4
votes
2 answers

Mocking System.getenv other than with powermock

Considering question https://stackoverflow.com/a/51980599/7203487. Only one method among methods in class contains System.getenv which needs to be mocked. Problem is I require to take jacoco code coverage which I get as 0% due to use of powemock.…
Gayathri
  • 1,776
  • 5
  • 23
  • 50
4
votes
1 answer

ClassCastException to disconnect HttpURLConnection casted from URLConnection with PowerMockito

My connection works fine with: URLConnectionFactory hadoopConnectionFactory = URLConnectionFactory.newDefaultURLConnectionFactory(cfg); String url = "..."; URLConnection urc = hadoopConnectionFactory.openConnection(new URL(url)); urc.connect(); //…
4
votes
1 answer

how to use System.getenv in Junit PowerMockito

I'm able to mock the System.getenv value from Junit but when I execute the test case - in my service class System.getevn value coming as null. Not sure what I'm doing wrong here. Please find my test service class and junit class. Can some please…
learn java
  • 231
  • 3
  • 14
4
votes
0 answers

Mocking Final Class Constructor

I want to mock final class' constructor. Example code is below. it doesn't work. it throw runtime exception. Please help me how to mock final class' constructor Thanks. I know this question quest, it doesn't work. All classes are not final Given…
ramazankul
  • 452
  • 1
  • 4
  • 14
4
votes
1 answer

Duplicate Answer processing with Mockito.doAnswer(...)

I am trying to test a static method that catches exception coming from different sub-calls or from the method itself and does just log the exception messages. By requirements, I cannot change the method implementation. Spying sub-calls is not a…
Maaaatt
  • 429
  • 4
  • 14
4
votes
2 answers

How do I mock a locally created object?

This is my class. class Test { public void print(){ RestTemplate restTemplate = new RestTemplate(); restTemplate.getForObject("url", String.class); } } to test this class, I want to mock "RestTemplate". Is there any way to…
santro
  • 373
  • 1
  • 6
  • 22
4
votes
1 answer

Mockito/PowerMockito - Mock a method that receives Lambda expression as a parameter

I want to mock the following method. But I don't find any Mockito.Matchers for the second parameter which uses Java.util.Function. public List convertStringtoInt(List intList,Function intToStringExpression) { …
RandomWanderer
  • 701
  • 2
  • 12
  • 23
4
votes
1 answer

Write unit test for okhttp timeout

I have written test case for okHttp SocketTimeoutException like this: @Test public void contactDetailsTimeOut() throws Exception { server.enqueue(new MockResponse().setBody("{}").setBodyDelay(31, TimeUnit.SECONDS)); HttpUrl baseUrl…
Anand Singh
  • 5,672
  • 2
  • 23
  • 33
4
votes
2 answers

Unit test for Runnable with Mockito

I have a code like this for which I would like to write unit test. public class TestClass { private final Executor executor; private final Handler handler; TestClass(Executor executor, Handler handler) { this.executor = executor; …
Ankit
  • 115
  • 1
  • 3
  • 12
4
votes
0 answers

Error while using powermock in JUnit

While I am running JUnit file I am getting the following type of error. Here is the complete stacktrace below: org.powermock.reflect.exceptions.FieldNotFoundException: Field 'fTestClass' was not found in class…
LPatil
  • 233
  • 3
  • 15
4
votes
2 answers

How to unit testing spring boot rest controller and exception handler using power mock

I am having a simple Spring boot application which contains Employee controller which returns the Employee names if the year passed is greater than 2014 and if the it is not less than 2014 then I am throwing a custom exception and handling it in…
Akash Jain
  • 145
  • 1
  • 3
  • 8