Unable to run JUnit test with PowerMockRunner

This is a bug that occurs when you use JUnit 4.12 and PowerMock < 1.6.1. The problem is solved in PowerMock 1.6.1. Please update your dependencies accordingly testCompile ‘junit:junit:4.12’, ‘org.powermock:powermock-core:1.6.1’, ‘org.powermock:powermock-module-junit4:1.6.1’, ‘org.powermock:powermock-api-mockito:1.6.1’ If you cannot upgrade PowerMock then you can use JUnit 4.11. testCompile ‘junit:junit:4.11’, ‘org.powermock:powermock-core:1.5.6’, ‘org.powermock:powermock-module-junit4:1.5.6’, ‘org.powermock:powermock-api-mockito:1.5.6’ Could you please add further lines of … Read more

Mocking Logger and LoggerFactory with PowerMock and Mockito

EDIT 2020-09-21: Since 3.4.0, Mockito supports mocking static methods, API is still incubating and is likely to change, in particular around stubbing and verification. It requires the mockito-inline artifact. And you don’t need to prepare the test or use any specific runner. All you need to do is : @Test public void name() { try … Read more

How to mock private method for testing using PowerMock?

I don’t see a problem here. With the following code using the Mockito API, I managed to do just that : public class CodeWithPrivateMethod { public void meaningfulPublicApi() { if (doTheGamble(“Whatever”, 1 << 3)) { throw new RuntimeException(“boom”); } } private boolean doTheGamble(String whatever, int binary) { Random random = new Random(System.nanoTime()); boolean gamble = … Read more

How do I mock a static method that returns void with PowerMock?

You can stub a static void method like this: PowerMockito.doNothing().when(StaticResource.class, “getResource”, anyString()); Although I’m not sure why you would bother, because when you call mockStatic(StaticResource.class) all static methods in StaticResource are by default stubbed More useful, you can capture the value passed to StaticResource.getResource() like this: ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); PowerMockito.doNothing().when( StaticResource.class, “getResource”, captor.capture()); Then … Read more

Mock a constructor with parameter

The code you posted works for me with the latest version of Mockito and Powermockito. Maybe you haven’t prepared A? Try this: A.java public class A { private final String test; public A(String test) { this.test = test; } public String check() { return “checked ” + this.test; } } MockA.java import static org.hamcrest.MatcherAssert.assertThat; import … Read more

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