How to mock a void static method to throw exception with Powermock?

Answer is as below. After consulting here http://code.google.com/p/powermock/issues/detail?id=278 , in fact Adder.add(12) above is part of setting up mock static method. It means when invoking Adder.add() with argument 12, IOException will be thrown. It is hard to understand, right? 🙂 So it should be written as below. PowerMockito.mockStatic(Adder.class); PowerMockito.doThrow(new IOException()).when(Adder.class); Adder.add(anyInt()); EDIT: Link is dead, … Read more

Unable to get Jacoco to work with Powermockito using offline instrumentation

This pom worked for me: <build> <finalName>final-name</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.2.201409121644</version> <executions> <execution> <id>default-instrument</id> <goals> <goal>instrument</goal> </goals> </execution> <execution> <id>default-restore-instrumented-classes</id> <goals> <goal>restore-instrumented-classes</goal> </goals> </execution> <execution> <id>default-report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin> </plugins> </build> See this … Read more

NoClassDefFoundError when using Powermock

I just solved this one now, when I added the @RunWith(PowerMockRunner.class) attribute, eclipse automatically imported: import org.powermock.modules.junit4.legacy.PowerMockRunner; All I needed to do is change it to be: import org.powermock.modules.junit4.PowerMockRunner; And now it works fine with JUnit 4.8.2. The 2nd runner is for when running with older versions of JUnit – specifically 4.3 and older.

PowerMock, mock a static method, THEN call real methods on all other statics

What are you looking for is called partial mocking. In PowerMock you can use mockStaticPartial method. In PowerMockito you can use stubbing, which will stub only the method defined and leave other unchanged: PowerMockito.stub(PowerMockito.method(StaticUtilClass.class, “someStaticMethod”)).toReturn(5); also don’t forget about the @PrepareForTest(StaticUtilClass.class)

AndroidStudio/Gradle with powermock

I’m posting in order to help future readers, you need to add these dependencies for powermock in AS testImplementation ‘junit:junit:4.12’ testImplementation ‘org.powermock:powermock-api-mockito:1.6.2’ testImplementation ‘org.powermock:powermock-module-junit4-rule-agent:1.6.2’ testImplementation ‘org.powermock:powermock-module-junit4-rule:1.6.2’ testImplementation ‘org.powermock:powermock-module-junit4:1.6.2’

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)