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

Powermock SerializationIssue

When I add the Powermock depenencies to my maven project I get the following error message: The project has the following dependencies: junit junit
Bastian
  • 4,638
  • 6
  • 36
  • 55
0
votes
1 answer

Mocking a static method with a generic parameter

I have the following setup : Class to test : SeriesOffset which extends BaseDisplayOption Test Class : SeriesOffsetTest When creating an object of the SeriesOffset class to test it, the constructor of the same makes a super call which then makes the…
Parth Mody
  • 438
  • 1
  • 5
  • 17
0
votes
1 answer

PowerMockito.doAnswer not producing the output

I've been trying to implement PowerMockito.Answer for awhile and still stuck.. here is the code PowerMockito.mockStatic(testLog.class); PowerMockito.doAnswer(new Answer() { public Void answer(InvocationOnMock invocation) throws Throwable…
0
votes
1 answer

PowerMockito private method doThrow stubbing

private int privateMethod(Object o, boolean b) throws Exception -- PowerMockito.doThrow(new Throwable("Just a throwable")).when(spy, "privateMethod", Mockito.any(), Mockito.any()); -- org.mockito.exceptions.misusing.UnfinishedStubbingException:…
Gobliins
  • 3,848
  • 16
  • 67
  • 122
-1
votes
1 answer

How to test a public method that calls a private method with a mocked object?

I am testing a class "DataSource" that extends another class "ExtendedDataSource". The method that I am testing in the "DataSource" class is calling another protected method in the "ExtendedDataSource" class. class DataSource extends…
Jeet
  • 359
  • 1
  • 6
  • 24
-1
votes
1 answer

I want to mock static RestTemplate object in postInvoiceByRestService method

Unable to mock Rest Template static object in my postInvoiceByRestService method I have tried lots of ways including Power Mockito Mockito spy but did not work for me What strategy need to use mock static Rest Template object? Java Class public…
Ritesh
  • 28
  • 5
-1
votes
1 answer

Adding junit for a static method call

Following is my class, where Display class has a static method resultDisplay(). Display class is coming from a jar file. I want to write junit for testCode() method. I am not interested in Display.resultdisplay() call. I just want to verify the…
Shankar
  • 113
  • 1
  • 14
-1
votes
1 answer

Misusing Unnecessary Stubbing Exception in Junit

I am writing a Junit test case using Mockito & PowerMockito in Java. Below are the code snippet: ApplicationCovrgeCommonRules class has method checkForValidFormIdForCoverageTest() which needs to be tested. This method is calling another method (of…
Raushan
  • 214
  • 2
  • 3
  • 10
-1
votes
1 answer

Junit test case for WebClient Builder for spring framework for reactive

Code for Get call using web client which I am trying to unit test webClient.get().uri("http://localhost:8080/employee/{id}", xyz).retrieve().bodyToMono(String.class);
-1
votes
1 answer

Getting "Null Pointer Exception" and "You cannot use argument matchers outside of verification or Stubbing" using indexAsync method in resthighlevelcl

@Repository public class ElasticRepository { private static final Logger LOG = LoggerFactory.getLogger(ElasticRepository.class); @Autowired private ElasticSearchConfig elasticSearchConfig; @Autowired private ObjectMapper…
Chinni
  • 1
-1
votes
1 answer

How to mock declared class in private method using mockito

I am writing junit test to test the private method which has class declared in it. My code: private Object getFunctReturn() { InterfaceName obj = new ClassName(): Map map = obj.getMap(); map.foreach()->{ //iterating map…
TechP88
  • 25
  • 1
  • 6
-1
votes
2 answers

In JUnit-5, How do I mock a static method that returns void? Can we use import org.mockito.MockedStatic?

I have a few 3rd parties static util methods in my project, some of them just pass or throw an exception. There are a lot of examples out there on how to mock a static method using PowerMock but Junit5 doesn't support PowerMock, which has a return…
Raghavendra S S
  • 115
  • 3
  • 12
-1
votes
1 answer

how to mock a mehod using spring boot SpringJUnit4ClassRunner where mongo tamplate is used where the method contains static call

i am sharing a method code which contains static method call. public List getTestContent() { DistinctIterable distinctIterable = mongoTemplate.getCollection("test-doc").distinct("testcontent",String.class); …
user14104736
-1
votes
1 answer

How to mock a private method called inside another private method

I was trying to mock a private method's output which is being called inside another private method, I have no choice but to test the later private method, so I have added sample test code which I can represent here, This Sample Class package…
-1
votes
1 answer

How to execute the failure scenario for the catch block of the method

Below is the code for which I want to write the test case , public static void createFileAndSave(String tag, String logText, File logFile) { try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(logFile, true))) { …
Shrut
  • 555
  • 7
  • 13