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
5 answers

Executing Initialization Code only once for multiple test classes

I am writing unit test cases for my code. I am using PowerMockito with Junit. I have written an initialization code which will take care of all the initialization stuff in my application. Below is the way my code is structured: Class…
Prerak Tiwari
  • 3,436
  • 4
  • 34
  • 64
4
votes
1 answer

Mock static method call in default method in java 8 interface

I have an interface like public interface WithMD5Calculator{ default String getMd5(){ try{ MessageDigest md = MessageDigest.getInstance("MD5"); //... not important }catch(NoSuchAlgorithmException e){ …
Koziołek
  • 2,791
  • 1
  • 28
  • 48
4
votes
1 answer

Unit test for Scala object (not class)

I've a singleton class in Scala that's declared as an 'object'. I need to write unit tests that 'mock' methods in this object. How do I do it? I tried using 'PowerMockRunner' etc with no luck. For example: object MyClass extends Serializable { …
DilTeam
  • 2,551
  • 9
  • 42
  • 69
4
votes
1 answer

java.lang.LinkageError: loader constraint violation in interface itable initialization:

I am getting this error when I try to mock JTextField. java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "javax.swing.text.JTextComponent$1.dropLocationForPoint( …
James Franco
  • 53
  • 3
  • 10
4
votes
1 answer

PowerMock: how to suppress parent methods on a Spy?

How do I correctly suppress a parent class method on a Spy? If I have a class Parent: public class Parent { public void method() { System.out.println("Parent.method"); } } class Child extends Parent { @Override public void method()…
Villa
  • 459
  • 7
  • 17
4
votes
2 answers

PowerMockito: java.lang.NoClassDefFoundError

I need to mock a static final class in my Junit test: private MyFinalClient client; @Before public void setup() { // set up mock client = PowerMockito.mock(MyFinalClient.class); service = new PatientLocationService(); …
Richard Hsu
  • 843
  • 1
  • 10
  • 15
4
votes
1 answer

TooManyConstructorsFoundException in Power mockito

I have tried to mock JSONArray and also tried by suppressing constructor. But none of the solutions are working for me. JSONArray mockJSONArray=PowerMokcito.mock(JSONArray.class);, …
Sai's Stack
  • 1,345
  • 2
  • 16
  • 29
4
votes
2 answers

PowerMockito how to throw exception on constructor calling

I'm testing following private method (getPrintWriter) with reflections: public abstract class CsvAuditor extends Auditor { public void run() { PrintWriter writer = getPrintWriter(); if (writer == null) return; …
Eleid
  • 65
  • 1
  • 7
4
votes
2 answers

How can I mock a private static method with PowerMockito?

This is the same question as found here. Unfortunately, the accepted answer isn't working for me. I have a static utility class with private methods that I need to test. I find that when I mock methods like…
user3897392
  • 397
  • 2
  • 5
  • 22
4
votes
1 answer

PowerMockito.whenNew isn't working

Update. Check working example in the end. I've got a class: package test; public class ClassXYZ { private final String message; public ClassXYZ() { this.message = ""; } public ClassXYZ(String message) { …
Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
4
votes
6 answers

Test a method without initialize the class

I'm very new to unit testing, I'm wondering if there is a way to test a method without initializing the class. The reason I'm asking is because there are lot of object passing in the constructor meaning a lot of mocking stubbing while from a…
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
4
votes
2 answers

PowerMockito not working : -java.lang.NoClassDefFoundError: org/mockito/internal/creation/CglibMockMaker

I am trying to mock the static method of CouchbaseCluster.create() using powermockito. Here is my test class. @PrepareForTest(CouchbaseCluster.class) public class IAMKafkaConsumerTest extends PowerMockTestCase { private IAMKafkaConsumer…
Puneet
  • 753
  • 3
  • 9
  • 24
4
votes
1 answer

Why mocking a private method enters method?

I'm using PowerMockito to mock a private method in my test. validator = spy(new CommentValidator(form, request)); PowerMockito.when( validator, method(CommentValidator.class, "isCaptchaValid", HttpServletRequest.class)) …
Sidney de Moraes
  • 993
  • 3
  • 11
  • 29
3
votes
1 answer

Logger.getRootLogger().addAppender(appender) removed in Test cases

I am migrating all old legacy Log4j1.x Test cases to Log4j2.x based test cases, I've a test case like below, which I am not able to convert, condition is I dont want to change anything in asserts. Note: I can't use Junit 5, because its legacy code,…
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186
3
votes
1 answer

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…