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.

Filter by
Sorted by
Tagged with
0 votes
0 answers
21 views

How to mock Constructor in mockito 5.2.0

I'm upgrading PowerMockito to Mockito 5.2.0, While upgrading, I am facing an issue with mocking the constructor. I have tried many ways to mock the constructor, Can someone let me know how we can ...
priyanshi sahu's user avatar
0 votes
0 answers
17 views

PowerMock not using mocked "new" object

I have a Spring Boot project and trying to use PowerMockito to mock an object that is created inside the method. I have some code that is declaring a new Object that is extending an Abstract class: @...
SS123's user avatar
  • 3
0 votes
1 answer
47 views

PowerMockito verifyStatic: validating async call to static method

I'm making call to an external system via a static method MyExternalServiceAccessor.myMethod(param1, param2); And so far I've been unit-testing above using PowerMockito's verifyStatic as follows ...
y2k-shubham's user avatar
  • 10.5k
0 votes
0 answers
27 views

CGLIB$SET_THREAD_CALLBACKS([Lorg.springframework.cglib.proxy.Callback) :

Some Junit Test cases are failing when running all test classes together but passing individually. **Caused by: java.lang.NoSuchMethodException: xxx.zzz$$SpringCGLIB$$0.CGLIB$SET_THREAD_CALLBACKS([...
Ashvinee Pawar's user avatar
0 votes
1 answer
41 views

Mockito mock static method

I need to test the method myMethod in the class MyClass and in order to do so I need to intercept the nextInt call to make return a specified value. How do I do so? import static org.apache.commons....
Poli's user avatar
  • 1
0 votes
0 answers
19 views

How to return different values with the same method call in the same class in a loop?

I am looping over a list table names to do the following Call describe table to check if the table exists If the table doesn't exist, throw ResourceNotFoundException. Call createTable method to ...
Mojo jojo's user avatar
0 votes
0 answers
17 views

all my jUnit tests in Jenkins return same error related to StackTraceCleanerProvider method isIn()

I inherited a Spring project that has about 160 failing JUnit Tests with Mockito classes When I check in Jenkins, I see that every single test has the following Error details: Receiver class org....
Kai Unewisse's user avatar
0 votes
1 answer
69 views

MockitoJUnitRunner throws NullPointerException while instantiating AppCompatActivity

I am trying to mock an Activity which extends AppCompatActivity using the MockitoJUnitRunner. I understand that I should use Instrumentation Test to test the activities in Android. Here I just want to ...
Raj's user avatar
  • 496
0 votes
0 answers
32 views

JUnit powermock throw exception IllegalAccessError:cannot clear JavaAgentClassRegister

In java,use powermock to test.There are 3 projects in my workspace. Two of them has been run without any problem.But last one throw Exception java.lang.IllegalStateException: Cannot clear ...
user22520757's user avatar
0 votes
1 answer
39 views

How to mock MainActivity using Mockito or another mocking framework?

I need to mock MainActivity as it is a parameter of an unit test I have. I tried private val mockMainActivity: MainActivity = mock(). However I get: Mockito cannot mock this class: class se.username....
xxrapunzelxx's user avatar
0 votes
1 answer
34 views

Mocked but still null when using PowerMock to mock private method

Here are the brief codes of my project, class Foo: public class Foo { @Autowired protected Mapper mapper; protected User getUser() { // ... return user; } } class Bar ...
vurtne yuan's user avatar
0 votes
1 answer
40 views

Call a real static method of a mocked class but modifying the parameters using powermockito

I'd like to call a real method of a mocked class, but modifying the parameters. I can't do any change in class that is being tested in order to better design, so I'm using PowerMockito to achieve it. ...
oitathi's user avatar
  • 153
1 vote
0 answers
24 views

Mock DriverManager using powermockito

public class Manager { public void testConnection() throws SQLException { Connection c = DriverManager.getConnection("", "", ""); c....
Vikas's user avatar
  • 11
1 vote
1 answer
45 views

How to mock overloaded public static method which returns void with PowerMockito

I got a problem mocking a public static method which returns void. This is what I have so far: @RunWith(PowerMockRunner.class) @PrepareForTest(TheUtilsClass.class) public class MyTest { @Test ...
Saša's user avatar
  • 4,526
-1 votes
1 answer
125 views

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 ...
Jeet's user avatar
  • 365
0 votes
0 answers
17 views

PowerMockito whenNew not invoked when constructor is called

Here are my classes. public class Foo { public Foo() { } } public class Bar { public Bar() { } public Foo getFoo() { return new Foo(); } } This is my test. @RunWith(PowerMockRunner....
Carol Jin's user avatar
0 votes
0 answers
46 views

Conversion of Powermockito Junit Tests to MockitoRunner

I would just like to ask if there's an easy way or manageable way to convert an existing Junit running on Power Mockito to Mockito Runner? The Powermockito doesn't seem to be supported for JDK 17 and ...
user3655258's user avatar
1 vote
1 answer
438 views

I'm creating unit test for a static class and it has method call of another static method from same class? How to can mock that method call?

How can I create unit test for testAble() method and mock the behavior of something() method? public class MyClass { public static String something() { return "Hello"; } ...
Gul Ahmed's user avatar
0 votes
1 answer
38 views

How to write test case using vertx for db class?

Below is my written code. I want to write test cases using powermokito for db class. I expect to write test cases for below vertx related classess using junit powermokito to cover 80% coverage. I ...
Kalpani Athukorala's user avatar
0 votes
0 answers
47 views

Powermock and JAXB causing javax.xml.bind.UnmarshalException

I am getting this error when I run the powermock test. I am using java 13 and gradle 6.5.1. Any suggestion with this really help. This is working fine with hibernate-core 5.3.21.Final and above but ...
Parag Kadam's user avatar
  • 3,680
1 vote
0 answers
116 views

How to write a test for a class which has a method and parameter in this method is of ServiceBusReceivedMessageContext?

I am not able to write test case for a class which has a function and that function has ServiceBusReceivedMessageContext as the parameter. ServiceBusReceivedMessageContext class is final class.Can ...
Shubham Singh's user avatar
0 votes
0 answers
20 views

Getting Error while writing the test cases

java.lang.NoClassDefFoundError: org/mockito/cglib/proxy/MethodInterceptor ServiceBusReceivedMessageContext is final class enter image description here Can anyone help on this? I am not sure why this ...
sf bazar's user avatar
0 votes
0 answers
32 views

PowerMockito mocking a method call

I want to test method A, inside there is a call to method B, both in the same class, inside method B are calls to other methods from other classes. I want to mock method B call, but Mockito also ...
Buntom's user avatar
  • 23
0 votes
1 answer
44 views

Problem on mock new Object using PowerMockito

Basically i have a class, TwsConnector, with 1 method, that provide a connection via REST call to a client that schedule a job. This is the class: @Service public class TwsConnector { @Autowired ...
EZDM's user avatar
  • 3
0 votes
1 answer
151 views

Why does StepVerifier.verifyComplete() is going into infinite loop when I use Scheduler and the test completes when I remove scheduler?

I am trying to test orchestrator flow which is using Reactor Mono. When I am testing the flow using StepVerifier the test was never finished. If I remove publishOn(myThreadDispatcher) then the test is ...
Lokesh Reddy's user avatar
0 votes
1 answer
51 views

Junit giving error related to private access in a class FilesMgr and as well as thenReturn(FileStatus)

Junit giving error related to private access in a class FilesMgr and as well as thenReturn(FileStatus) Hello All, Here is the below original class, I've written Junit for getFilesWithSizes() method. ...
Dheeru's user avatar
  • 1
0 votes
0 answers
26 views

Convert powermockito testcase code into testng testcase

Here I am using whenNew method in powermockito. @Test public void test() { MsgTransmit msgtransmit= mockm(MsgTransmit.class); String userId= id101; Powermockito....
Keerthi Senthil's user avatar
0 votes
1 answer
59 views

mock an internal method with Mockito Junit test

I'm really lost with my error, I saw a lot of documentation and not able to find my error. I'm testing a method service, which has a class and method inside: @Service public class myServiceImpl ...
cucuru's user avatar
  • 3,494
0 votes
0 answers
20 views

How to verify with PowerMockito.verifyStatic my method that I am testing in java [duplicate]

I am trying to verify my 3 methods that I am testing, only that I use my PowerMockito.verifyStatic in 3 different test methods and apparently that is the problem, that it does not let me test the 3 ...
Jonathan Ortiz's user avatar
0 votes
1 answer
93 views

How do I mock methods of a private final client that's being initialized inside a dependency class?

I have a class that looks like this : class A { private final B b; public A(String x, String y){ b = new B(x,y); } public void someMethod() { b.externalMethod(); }...
abhi gang's user avatar
0 votes
0 answers
133 views

java.lang.NullPointerException: Cannot invoke "software.amazon.awssdk.http.async.SdkAsyncHttpClient.execute

Getting java.lang.NullPointerException: Cannot invoke "software.amazon.awssdk.http.async.SdkAsyncHttpClient.execute(software.amazon.awssdk.http.async.AsyncExecuteRequest)" because "this....
Nibedita Behera's user avatar
0 votes
1 answer
424 views

How to mock a static method in scala?

I have a static method in scala and by static method I mean to say that this method is inside an Object named as MyObject. I also have a class named as MyClass where there is a method which calls the ...
gaurav narang's user avatar
0 votes
0 answers
56 views

how to mock a protected method in an abstract class called from BaseClass

Updating the original question. I will put the original code piece and then put in questions. Class under test package my.main.class.package; public class MyClassHelper extends MySession { //Session ...
Abhinash Jha's user avatar
-1 votes
1 answer
134 views

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 ...
Ritesh's user avatar
  • 28
0 votes
1 answer
129 views

Mocking: Verify Static Void Method Is Called

I am having trouble with the test cases for methods: deleteOldBermudaFolder and isExistingDirectory. Below is the code: class BermudaCacheDumpServiceImpl implements BermudaCacheDumpService { @...
maiah's user avatar
  • 23
0 votes
1 answer
54 views

How to mock default method of unimplemented interface?

I'm doing some hands on with Junit/Mockito/PowerMockito I have an interface class import retrofit2.Call; import com.learning.model.user.User; import java.io.IOException; public interface UserService {...
Đức Trường Đặng's user avatar
0 votes
0 answers
58 views

PowerMockitoExtension.class not found

I am using this for my new test cases @ExtendWith(PowerMockitoExtension.class) i have added dependencies in my grade as follow testImplementation 'io.quarkus:quarkus-junit5' testImplementation 'io....
Abhishek Khaiwale's user avatar
0 votes
0 answers
466 views

@Mock not working to mock @Component class in springboot

I am trying to create a Mock instance for spring Component class. Below is the spring component class. @Component public class MyComponentMapperClass { @Value("${some.property}") private ...
Abhinash Jha's user avatar
0 votes
1 answer
72 views

How to create a mock object when it doesn't have the default constructor?

I am working on Junit by using powermockito. I have sample code like below: class A { private B b; A() { b = new B("Test"); } public void invokeAnotherClassMethod() ...
Jyothi Sony's user avatar
0 votes
0 answers
21 views

How to mock class which is called by another thread?

I have a class called BlobService which is called inside thread.And now when I try to mock,it doesn't get mocked.Instaed it gives error,that give proper connection string.And outside thread,it is ...
maitri sheth's user avatar
1 vote
0 answers
54 views

PowerMock.VerifyStatic alternate in mockito

PowerMock.verifyStatic(Util.class) I use this method in powermockito to verify this class, What is the method I need to use in mockito to verify the similar behaviour ?
Aakash V's user avatar
0 votes
1 answer
353 views

Error while mocking HttpUrlConnection call in REST service using Mockito in Java application

I am new to Java. I wanted to write unit test for the following method using Junit and Mockito framework. This method takes in payload, establish the http connection and return response: public static ...
Anand_Pawar's user avatar
0 votes
0 answers
17 views

How to write Junit for global/instance variables for Hook or interceptor classes

I have below code in project : public class KarateHook implements RuntimeHook { private Set<String> scenarios = ConcurrentHashMap.newKeySet(); private static final Logger logger = ...
Jyothi Sony's user avatar
0 votes
1 answer
266 views

How to access Kotlin static method in Groovy Spock test

I have kotlin class below class DPCIValidator { companion object { fun validate(value: String): Boolean { val regex = Regex(pattern = "\\d\\d\\d-\\d\\d-\\d\\d\\d\\d") ...
user3534759's user avatar
0 votes
0 answers
118 views

Need Power mockito compatible maven dependency version with existing Mockito maven dependency

I am using below Mockito maven dependency: <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>1.10.19</...
Coder001's user avatar
0 votes
0 answers
81 views

Why is the PowerMock.whenNew not working here?

I have this simple class: import java.io.File; public class TestPowerMock { public File createFileAndReturnIt() { return new File("/"); } } And this simple test: import ...
Omegaspard's user avatar
  • 1,860
0 votes
0 answers
143 views

Mocked Static HttpClient.newBuilder() returns null

The following is the test method where I am trying to return a mocked HTTPClient object, however after static mocking the HttpClient class, the HttpClient.newBuilder() step returns null @RunWith(...
Shivayan Mukherjee's user avatar
1 vote
0 answers
569 views

Powermock Failed to find the "modifiers" field in method setInternalState

I am trying to mock static class with Powermock. I am using JDK 14. However I am getting the below error: Internal error: Failed to find the "modifiers" field in method setInternalState. ...
Spartacus's user avatar
  • 365
0 votes
0 answers
18 views

How we can mock successive calls of a private method being called from a public method of class being tested

Could not mock multiple return value for a private method of class under test, being called from public method of same class. I am using PowerMockito in java to mock multiple return values from a ...
sonu mourya's user avatar
0 votes
1 answer
44 views

Not getting how to write power mock test cases for below use case

I am having one java code scenario but not getting how to write power mock test cases for that. Class Test { ClosableHttpClient c; } Class MainTest extends Test{ // code for httpPost and ...
JohnD's user avatar
  • 5

1
2 3 4 5
27