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
3
votes
3 answers

How to mock getResourceAsStream method using PowerMockito and JUnit?

I try to mock getResourceAsStream method which I invoke in constructor. public Conn() { stream = Conn.class.getClass().getResourceAsStream(PATH); } For mock framework I prefer Mockito + PowerMockito. @RunWith(PowerMockRunner.class) …
barbara
  • 3,111
  • 6
  • 30
  • 58
2
votes
1 answer

Mocking of static field that is a class with Mockito

My Utils class has an init method dependent upon external resources (e.g., a database connection) that I am unsuccessfully mocking with Mockito. Utils seems like it can be share across instance of my application, so is declared as a static (i.e., a…
Dr Dave
  • 550
  • 1
  • 6
  • 22
2
votes
2 answers

PowerMockito verifyTimes Always passets private method

public class TestedClass { public void publicMethod() { privateMethod(); } private void privateMethod() { } } I would like to test with PowerMockito that the private method is called exactly once. Here's…
STSISA
  • 61
  • 4
2
votes
1 answer

Fail to restTemplate create with @PowerMockIgnore in powermock

I need a powermock for private method test. It is working well if I use only @RunWith(PowerMockRunner.class) without @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class). I need to use @PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class) for…
bittap
  • 518
  • 1
  • 6
  • 15
2
votes
1 answer

NullPointerException when powermockito testing method invocation

I'm new to unit testing with powermockito and currently i'm trying to test a method that calls java.util.Timer.scheduleAtFixedRate. I've tried to run the test but it failed and showed NullPointerException at the line where an object invoke a method…
Alephaze
  • 43
  • 6
2
votes
1 answer

How to mock Constants class

Constants { public static final PATH = "/contentFiles"; // Other constants ... } How do I mock the PATH field in my Constants class? I want to mock the PATH variable to set it to be something along the lines of "/test/contentFiles". I want…
Simon Pedersen
  • 43
  • 1
  • 1
  • 8
2
votes
1 answer

How to inject only one mock inside Service class with normal instantiation to other instances?

Assume i have implemented next service class: @Service @RequiredArgsConstructor @Transactional public class ItemService { private final ItemRepository itemRepository; private final ItemCreatorFactory itemCreatorFactory; private final…
mibrahim.iti
  • 1,928
  • 5
  • 22
  • 50
2
votes
2 answers

Mock ContextLoader.getCurrentWebApplicationContext() call using when in mockito .How can i do that?

I am trying to mock ContextLoader.getCurrentWebApplicationContext() call in when using mockito but its fails to mock. //here is my source code @Mock org.springframework.web.context.WebApplicationContext…
2
votes
2 answers

How to cover custom exception when catching an exception in mockito in Java

For eg. try { // Some code } catch (IOException e) { throw new CustomDefineException(e); } I want to write mockito coverage statement for the catch block. How can that be done? I'm new on mockito framework.
2
votes
0 answers

Powermock or PowerMockito with JUnit 5

Is somebody able to direct me to one solid piece of documentation that outlines how to integrate JUnit 5 with Powermock framework, specifically the PowerMockito class? I see many people having issue with this on stack overflow and multiple other…
2
votes
0 answers

Using PowerMockito to mock a class with @Inject variables and static methods

I am trying to use a power mock to mock a class with @Injects and static methods called in another class constructor. But for some reason it seems to be throwing MissingMethodInvocationException in PowerMockito.when. @Named public class…
Santosha
  • 21
  • 3
2
votes
2 answers

java.lang.SecurityException: signer information does not match signer information of other classes in the same package in PowerMock

When using PowerMockito.mockStatic(Test.class), where Test.class is part of the jar which is signed, I am getting SecurityException. This happens at java.lang.ClassLoader.checkCerts(ClassLoader.java:898) Is there anyway to avoid cert check in…
Lokn
  • 421
  • 4
  • 17
2
votes
0 answers

Failed to transform class with name org.apache.hadoop.fs.FileSystem$Cache. Reason: org.apache.hadoop.fs.FileSystem$Cache$Key class is frozen

Getting java.lang.RuntimeException: org.apache.hadoop.fs.FileSystem$Cache$Key class is frozen. Java : 11 JVM vendor name : Oracle Corporation JVM vendor version : 11.0.7+8-LTS JVM name : Java HotSpot(TM) 64-Bit Server…
2
votes
1 answer

Mokito - java.lang.NoClassDefFoundError: org/powermock/core/classloader/MockClassLoader

I have been spending time in resolving this error and looked for references, tried out but still no luck. Tried with MockitoAnnotations.initMocks, cleared the old version of Mockito. How to fix this? Dependencies: My…
Aavik
  • 967
  • 19
  • 48
2
votes
1 answer

How to test/Junit class using AnnotationConfigApplicationContext

I have a servlet that makes use AnnotationConfigApplicationContext , I want to test this class. How do i mock AnnotationConfigApplicationContext or is there way to test below class. I dont want to use spring-auto-mock due to very specifics…
sumedha
  • 473
  • 1
  • 9
  • 24