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.

PowerMock is a Java unit testing library that takes mock creation further than is possible using Mockito and EasyMock and facilitates mock creation and behaviour in areas that they deem un-testable.

Facilities available from PowerMock include:

  • mocking static, private and final methods
  • partial mocking, and
  • mocking construction of new objects

The project is open source on Apache License 2.0 and is available in Git Hub.

2039 questions
15
votes
2 answers

PowerMock's expectNew() isn't mocking a constructor as expected

I'm trying to learn the ins and outs of various mocking libraries and PowerMock(specifically the EasyMock extension) is next on the list. I'm attempting to mock a constructor and the examples provided don't have the same response when I try to…
AdamSpurgin
  • 951
  • 2
  • 8
  • 28
15
votes
2 answers

issues while using @RunWith Annotation and powerMock

Initially I was using only Mockito in junits so I was using SpringJUnit4ClassRunner.class in @RunWith annotation ie @RunWith(SpringJUnit4ClassRunner.class) due to which spring dependency injection was working fine and was getting a bean…
Bhuvan
  • 2,209
  • 11
  • 33
  • 46
14
votes
4 answers

java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework

I've been facing a peculiar problem. Basically, when I run my Mockito test normally i.e. 'Run as Junit Test', it gives me the following error. Can someone help me please what is my mistake? The error received: java.lang.NoSuchMethodError:…
Krish
  • 4,166
  • 11
  • 58
  • 110
14
votes
2 answers

ClassCastException exception when running Robolectric test with Power Mock on multiple files

So I set up the power mock based on the reference guide here. It all seems to run perfectly fine with a single test class. But when executing multiple JUnit tests I am getting the following error on the second test class. As you can see from the…
14
votes
4 answers

Verify that exception was caught with Mockito and PowerMock

Is there any way to verify that exception was caught? I mean that in my method there is a situation when I should catch the exception and in test I want to verify that exception was really caught.
Anatoly
  • 1,551
  • 4
  • 21
  • 40
14
votes
1 answer

Mocking behaviour resets after each test with PowerMock

I'm writing unit tests using PowerMock, mocking behaviour of some util classes. Defining behaviour once for test class (by @BeforeClass annotation) causes: first test invocation to return mocked value second test invocation to return real method…
Kamil Seweryn
  • 2,072
  • 2
  • 17
  • 23
13
votes
4 answers

Why EclEmma doesn't coverage code with tests with @RunWith(PowerMockRunner.class)

I am using EclEmma with Eclipse to help me to know where is missing code tests in my project, but all tests with @RunWith(PowerMockRunner.class) are not called and thus not tested. I´m using JUnit 4.8.1 with Mockito. What could it be?
LottaLava
  • 889
  • 1
  • 9
  • 21
13
votes
3 answers

java.lang.LinkageError: ClassCastException

I do experience a really annoying problem with TestNG and RESTeasy. I do have a class that runs several tests against an API class which uses the RESTeasy framework to expose itself. However if I let the test run with maven (mvn test), then I get…
rit
  • 2,308
  • 3
  • 19
  • 27
13
votes
2 answers

Powermock complains about default.properties file found in 2 places

I'm using Robolectric to test my app, I decided to use Powermock to mock static and final classes so I added the following dependencies to my build.gradle file: testImplementation "org.powermock:powermock-module-junit4:1.7.0" testImplementation…
glisu
  • 1,027
  • 10
  • 20
13
votes
3 answers

Testing code which calls native methods

I have a class like this: public final class Foo { public native int getBar(); public String toString() { return "Bar: " + getBar(); } } Please note that getBar() is implemented with JNI and that the class is final. I want…
kayahr
  • 20,913
  • 29
  • 99
  • 147
13
votes
4 answers

Mockito.any returns null

I am trying to mock a static method with parameters like this : Mockito.when(StaticClass.staticMethod(Mockito.any(A.class), Mockito.any(B.class), SomeEnum.FOO)) .thenReturn(true); I have added the…
Eko
  • 1,487
  • 1
  • 18
  • 36
13
votes
2 answers

@PowerMockIgnore at project level

I have following error in my powermock test cases while running in Maven : java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type…
abhig
  • 840
  • 2
  • 12
  • 22
13
votes
2 answers

PowerMockito: NotAMockException on a mock

Bit of a complicated setup. Robolectric, PowerMockito rule-based config. @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) // Using…
Jon O
  • 6,532
  • 1
  • 46
  • 57
12
votes
4 answers

How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?

I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream: public static final FilterFactory BZIP2_FACTORY = new FilterFactory() { public InputStream makeFilter(InputStream in) { // a lot of other…
Zack
  • 6,232
  • 8
  • 38
  • 68
12
votes
2 answers

Use doReturn to partially mock static method with PowerMockito

How to I use the doReturn pattern in PowerMockito to mock a static method when I can't use Mockito.when()? I want to test the following static method: public static PrintWriter openWriter(File file, Charset charset, boolean autoflush) throws…
Zack
  • 6,232
  • 8
  • 38
  • 68