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
8
votes
2 answers

Exception using SpringRunner with PowermockRunner

I am trying to test a JavaMail api and using SpringRunner and PowerMockRunner but it is failing. @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(SpringRunner.class) @PowerMockIgnore(value = {"javax.management.*"}) @SpringBootTest public…
Abhishek Kumar
  • 3,328
  • 2
  • 18
  • 31
8
votes
3 answers

How to mock objects in Kotlin?

I want to test a class that calls an object (static method call in java) but I'm not able to mock this object to avoid real method to be executed. object Foo { fun bar() { //Calls third party sdk here } } I've tried different…
aloj
  • 3,194
  • 7
  • 27
  • 38
8
votes
2 answers

checked exception is invalid for this method

I have the below class There is an answer to this in StackOverflow but it deals with List throw checked Exceptions from mocks with Mockito. I like to look into this condition. Not getting where I am missing. public SimpleClass{ …
Sawyer
  • 423
  • 1
  • 7
  • 23
8
votes
2 answers

Powermock static final method in final class

The Test case I am writing for: public class AClassUnderTest { // This test class has a method call public Long methodUnderTest() { // Uses the FinalUtilityClass which contains static final method …
Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
8
votes
1 answer

PowerMockito mock single static method and return object inside another static method

I have written test cases to mock static classes and methods using PowerMockito's mockStatic feature. But I am strugling to mock one static method inside another static method. I did see few examples including this but none of them actually helping…
Extreme
  • 2,919
  • 1
  • 17
  • 27
8
votes
2 answers

Mocked object still calling method (Mockito + Kotlin)

I'm trying to test this class: class LoginPresenter(val mPostman: Postman) : ContractLoginPresenter, Validator.ValidationListener { private var view: ContractLoginView? = null override fun setView(_view: BaseView) { view = _view as…
E. Fernandes
  • 3,889
  • 4
  • 30
  • 48
8
votes
2 answers

PowerMock AmazonS3Client Config Issue

I'm getting this stack when trying to run a Mock test using PowerMock Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.amazonaws.services.s3.AmazonS3Client]: Factory method 'amazonS3Client' threw…
Joel Holmes
  • 943
  • 2
  • 12
  • 23
8
votes
2 answers

Mocking a private constructor

The Site class is provided to me by an external team and has a private constructor. public class Site { int id; String brand; private Site(int id, String brand) { this.id = id; this.brand = brand; } } The SiteUtil class…
Pranav Kapoor
  • 1,171
  • 3
  • 13
  • 32
8
votes
3 answers

Unable to mock URL class using PowerMockito/Mockito

I am trying to use PowerMockito to mock the creation of the java.net.URL class in my code that I'm testing. Basically, I want to prevent the real HTTP request from occurring and instead 1) check the data when the request is made and 2) supply my…
Jim
  • 119
  • 4
  • 14
7
votes
1 answer

Mockito junit 5 mock constructor

I want to mock a constructor and return a mock object when the constructor is called. This can be achieved using powermockito's whenNew method like this. PowerMockito.whenNew(ClassName.class).withAnyArguments().thenReturn(mockObject); Since Junit5…
Aeron Storm
  • 99
  • 1
  • 1
  • 11
7
votes
2 answers

Running powermock + mockito on java 11 http client

I am trying to run powermock + mockito with Java 11 for unit test cases. I am using the below versions: testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'org.mockito', name: 'mockito-core', version: '2.28.2' testCompile…
Subhomoy Sikdar
  • 526
  • 5
  • 19
7
votes
0 answers

Mocking an object in Scala using PowerMock

I'm trying to unit test one of my methods. Inside this method it has a line: val dynamicFrame = DynamicFrame(dataFrame, glueContext) The issue is that DynamicFrame is an object: object DynamicFrame { ... methods() } and I'm trying to use…
Victor Cui
  • 1,393
  • 2
  • 15
  • 35
7
votes
1 answer

Testing Annotation based RequestInterceptor

I wanted to do so custom logic(Record the request and response) on some routes. Based on some research I came decided to use AnnotationBased RequestInterceptor. This is my code for interceptor. public class CustomInterceptor extends…
ayushmad
  • 619
  • 1
  • 6
  • 9
7
votes
1 answer

Verifying arguments passed to static method with Mockito and PowerMock

I'm trying to test a void method though verifying the arguments it passes to a static method when it calls it. The static method is in charge of persisting those arguments. class ProxyHandler { public void process(String str) { // parse the…
Sam
  • 376
  • 3
  • 13
7
votes
1 answer

TestNG + Spring + Power mock Unit test

I have a Spring based application and am in the process of unit testing it. I'm using TestNG for unit tests. For my test i need to make use of PowerMockito to mock some static methods. I also need to make use of a test spring config file for only my…
user320550
  • 1,093
  • 6
  • 20
  • 37