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.
1,998
questions
0
votes
0
answers
11
views
Exception using Transformer and PowerMockRunner
I want to convert Document to String. I'm using the following method:
public static String toString(Document doc) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
...
0
votes
0
answers
26
views
How to mock an object in a static method?
I have questions regarding how to mock an object in a static method? Below is my code.
public class Helper {
private static final Map<String, List<String>> regionMaps = loadRegions();
...
0
votes
1
answer
24
views
How to get rid of Powermock annotations properly?
I'm migrating my project's tests to JUnit 5 and I want to replace Powermock with Mockito. But in one case I couldn't find out the appropriate equivalents for the following annotations:
@...
0
votes
0
answers
15
views
How can I use Poermock mocking local a variable?
I'm using Powermock to mock local variable "response" in sendAddTaskResult method.
public void sendAddTaskResult() {
//....
HttpResponse response
= request.header("Authorization&...
1
vote
1
answer
52
views
PowerMockito mock static didn't work correctly
I have a class need to be tested:
public class TestClazz {
public void abc() {
List<String> sa = Collections.singletonList("sa");
Stream<PropertySource<?>&...
0
votes
1
answer
37
views
NullPointer when mock an inject attribute using PowerMock
I'm testing a class using PowerMock, and I'm getting null pointer exception when a field that would be mocked is null. I'm mocking two fields, one to Golf and another to Tango. Golf is mocked fine, ...
0
votes
0
answers
73
views
java.lang.NoClassDefFoundError: Could not initialize class javax.xml.transform.FactoryFinder
I am upgrading my spring boot application from jdk8 to 11.
I am facing issue while running the test cases java.lang.NoClassDefFoundError: Could not initialize class javax.xml.transform.FactoryFinder.
...
-1
votes
0
answers
28
views
How to make a static method call throw an error in Java
so I have been using power mock to mock a static method call for unit testing in my java code. I want the same method call to throw an error now, however, when I just do this, expect().andThrows(new ...
0
votes
0
answers
26
views
PowerMock is not intercepting new Instances
Hi I´m trying to test some legacy code that uses command patron. I couldn´t make work PowerMock propperly and I tryed to make a simple example to test if it could work.
I think that the problem is ...
1
vote
1
answer
80
views
Caused by: java.lang.RuntimeException: java.io.IOException: invalid constant type: 18
I am migrating old legacy app from Log4j1.x to 2.x and getting below error while running the test cases. I already went through link: Powermock - java.lang.IllegalStateException: Failed to transform ...
0
votes
0
answers
69
views
How do we mock a method A of class z which is called inside a private method B of Class C and call private method B from Junit using powermock?
How do we mock a call to method A of class z which is inside a private method B of Class C and call private method B from Junit using powermock ?
Call to method A is inside private method B has to be ...
1
vote
1
answer
92
views
Junit 5 use mockConstruction().withSetting().useConstructor() instead of PowerMock.whenNew().withArguments()
Now I want to use Junit 5 + Mockito 4.x version + Mockito-inline 4.x Version instead of Junit 4 + PowerMock 2.0.9
Because the Junit 5 doesn't support PowerMock also Mockito-inline can mock static, ...
0
votes
0
answers
37
views
PowerMock whenNew, thenReturn equivalent in Mockito and JUnit5
We are migrating from PowerMock and JUnit 4 to Mockito and JUnit5. I have a very simple setup class.
@BeforeEach
public void setUp() throws Exception {
service = new ...
1
vote
1
answer
26
views
One of my mock reference becoming null in main class
For a reference client mock is prepared and available in test method but when it comes to myMethod the client value becoming null
class MainClass {
public void myMethod() {
HttpConnector ...
0
votes
1
answer
38
views
No calls to mocked object in scala using powermock
I am trying to use power mock in scala.
Class I want to test
class Operations {
def write(s: String): Unit = {
val customWriter = new CustomWriter()
customWriter.write(s)
}
}
Class I ...
0
votes
2
answers
52
views
How to mock Function interface from java 8 and System class
I want to mock below call :
((Function<String, String>) System::getenv).apply(TransformerConstants.TRANSFORMER_POOL_SIZE)
I tried using below code :
PowerMockito.when(((Function<String, ...
0
votes
1
answer
39
views
java.lang.linkageerror.....(instance of org/powermock..)
While running my JUnit test class, I am getting the following-
ERROR Statuslogger Could not reconfigure JMX
java.lang.LinkageError: loader constraint violation: loader(instance of org/powermock/core/...
1
vote
1
answer
31
views
use Powermock mock new Date() does not work as expected
I want to mock constructor of Date object. But it doesn't work as I expected.
suppose I have such a service.
public class TestService {
public Date getNewDate(){
return new Date();
}
}
...
0
votes
0
answers
24
views
Junit for Files.newDirectoryStream return NullPointerException
I'm working on a Junit test which is supposed to mock Files.newDirectoryStream static method. My test files are kept in the resources folder and that's the path I'm passing to my function too.
Here is ...
0
votes
1
answer
44
views
Unit Test For methods using this this keyword in function
I wanted to know how to write unit test cases for methods involving this keyword.
DispenseRuleLogic contains these two methods in which one is calling another function using this keyword. So I wanted ...
0
votes
0
answers
34
views
How to mock URL connection class using powerMock
This is my main method for which I want to write Junit
private HttpURLConnection createRedirectURL(String childNode) throws IOException {
URL url = new URL(childNode);
...
0
votes
1
answer
149
views
org.powermock.api.mockito.ClassNotPreparedException when it's declared to be prepared
I don't understand why this is showing since my test definitely asked PowerMock to prepare for it.
@PrepareForTest({AmazonSQSClientBuilder.class})
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({&...
1
vote
1
answer
57
views
Mocking an object returned from private method in java
I have a requirement where i want to mock an object that is return from private method. I am working with junit and mockito in my project. I can not paste the actual code here but sample code will ...
1
vote
1
answer
646
views
java.lang.NoSuchMethodError: org.mockito.Answers.get()Lorg/mockito/stubbing/Answer;
I am trying to upgrade the spring boot version from 2.2.4 to 2.6.6. I'm able to compile the application successfully and I'm able to start the application. But when i'm trying to run the tests, all ...
0
votes
0
answers
20
views
Matcher overrides other arguments
When I try to test below params, It throws different arguments exception. But when I update the document argument from anyString() to "{"date":"2022-10-17"}" test ...
0
votes
0
answers
18
views
How to prevent surefire from dividing test files between testng-native-results and testng-junit-results
We are using testng for testing, and I added a new class using powermockito (the test class extends PowerMockTestCase)
However we got an exception with another class that uses powermocks's ...
1
vote
1
answer
601
views
What is the way around this error "java.lang.RuntimeException: PowerMock internal error: Should never throw exception at this level"
java.lang.RuntimeException: PowerMock internal error: Should never throw exception at this level
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$...
0
votes
0
answers
85
views
How to mock file, java.nio.Path, Paths?
I have a method that has a few static classes as well as java.nio.Files,ResourceUtils, etc I am not sure how to write test cases for it as I keep getting NPE Paths.get(filePath);
Below is the method:
@...
1
vote
1
answer
129
views
Unable to mock System class static method using PowerMockito
Even though I have read the manual and gone through multiple answers for Powermock, could not mock a static method for my use case.
Class:
@Component
public class SCUtil{
public void ...
0
votes
1
answer
53
views
Need syntax to call powermock whitebox()
In my unit tests I'm trying to execute a private method using Whitebox.invokeMethod, but I don't know the syntax that works. I want to invoke this (getExcelMappingJsoDto() is the private method) but ...
0
votes
1
answer
67
views
How to mock private method using powermock?
If I have a simple class like:
public class LuckyNumberGenerator {
public int getLuckyNumber(String name) {
return getDefaultLuckyNumber();
}
private int getDefaultLuckyNumber() { }...
-1
votes
1
answer
145
views
mocking super class protected method returns null for @Mock on class variables
I am trying to write a unit test for a most common scenario.
class A{
protected void m1() {
//something
}
}
//class A is from a different/external binary
class B extends A {
@Autowired ...
0
votes
0
answers
112
views
ObjectMapper throwing exception while Mocking
Here is my usecase.
I am trying to write a junit for once class which have ObjectMapper as static instance variable.
But I am running in error. I am not able to figure out where I am going wrong.
@...
2
votes
0
answers
212
views
Using Mockito 4.3.1 instead of Powermock
I am trying to get rid of Powermock and mock my static classes with Mockito using version 4.3.1. I updated version in my dependencies. I updated my dependency as mockito-inline instead of mockito-core ...
0
votes
2
answers
146
views
NotAMockException exception when trying to verify a static method with Powermockito
I use PowerMock to test a static method as mentioned its documentation.
As far as I see, there may be a bug, but I am not sure:
Static mocking broken for Mockito >= 2.26.1
...
I tried the ...
0
votes
0
answers
108
views
PowerMock: Argument passed to verify() is of type Class and is not a mock
I am trying to test a static method using PowerMock as mentioned its documentation.
However, when I try to verify the following method as shown below, I get "Argument passed to verify() is of ...
0
votes
1
answer
145
views
Cannot verify static method in Unit Test using MockedStatic or PowerMockito
I am really fed up with by trying tens of different approaches to test a static method properly using MockedStatic or PowerMockito for several days.
I have the following service methods and test ...
0
votes
0
answers
205
views
How to use PowerMock in JUnit5?
As mentioned on some articles like Simplifying Junit, Mockito And PowerMock, I cannot use PowerMock in JUnit5.
As I cannot downgrade my JUnit version to 4 and need to stick with JUnit5, how can I use ...
1
vote
1
answer
163
views
PowerMockito.verifyStatic method does not work
I am trying to use PowerMockito in order to test a static method in my service. I have the following test method:
@PrepareForTest(LoggingUtils.class)
@RunWith(PowerMockRunner.class)
public class ...
0
votes
0
answers
128
views
can we mock private , static method in java 11 without powermock since powermock not support java 11
I have legacy code and i am using power mock for mocking the private and static method but power mock does not support java 11. I want to use some other tools or any workaround to mock the private and ...
0
votes
0
answers
42
views
How to mock a local variables using mockito/powermock
I have a function as follows :
private Response getHttpResponse(String url) {
WebTarget target = client.target(url);
Invocation.Builder requestBuilder = target.request().accept(...
0
votes
0
answers
27
views
How to test callback (interface) from a Custom Dialog to fragment using Robolectric and mockito in android?
I have a custom dialog class in android something like this
class MyDialog extends Dialog {
MyCallback callback;
private Button mBtn;
public MyDialog(MyCallback callback) {
this....
1
vote
0
answers
191
views
How to resolve the following error related to powermockito: "Mockito cannot mock this class: class org.apache.hadoop.fs.FileSystem."
This is the entire error:
Mockito cannot mock this class: class org.apache.hadoop.fs.FileSystem.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this ...
0
votes
1
answer
16
views
jUnit - test that the object is NOT created in main()
This may be a stupid question, as I'm just starting out with jUnit testing, but I need to test that an object of a class is NOT created in the main() method.
Basically, what I need is the opposite of ...
0
votes
1
answer
97
views
Mocking a Simple DateFormat
I have a variable currentDate = new SimpleDateFormat("ddMMyyyy_HHmmss",Locale.ENGLISH).format(new Date()); inside a class
In testcase I need to mock it. I have tried like this
...
1
vote
0
answers
566
views
What is the compatible version of Junit 4, powermock and springboot version 2x
I'm currently using versions as :
springboot-version : 2.0.1.RELEASE
Junit-version : 4.12
Powermock-module-junit4-verison : 1.6.4
Powermock-api-mockito-version : 1.6.4
I need a compatible version for ...
0
votes
1
answer
320
views
Gradle 7.3.3 run Junit unable to create TestEngine
I am using Gradle 7.3.3, but require JUnit and PowerMock
I have configured settings.gradle as:
version('spring', '5.3.15')
alias('spring-test').to('org.springframework', 'spring-test')....
0
votes
0
answers
52
views
PowerMock expectNew creating real objects and won't return mocked instance
I'm working with PowerMock to return the mock object of a private method but It never mocks the constructor define in private method and just proceeds as if it were normal.
Class Util {
public void ...
0
votes
0
answers
20
views
PowerMock: how to force parent's protected method to throw an exception
How can I mock my parent protected method to throw an exception using Mock/PowerMock?
class Parent {
private void parentMethod(param1 p) throws CustomException {
if(.....)
throw ...
0
votes
1
answer
69
views
Mocking call to static method call inside spy method
Pretty new to mockito. Stuck with a basic thing:
@Override
public void updateMeeting(Meeting meeting, Meeting oldMeeting, List<MeetingOccurrence> oldMeetingOccurrences, List<...