powermockito

Error trying to mock constructor for ProcessBuilder using PowerMockito

喜夏-厌秋 提交于 2019-12-10 10:46:01
问题 I am trying to mock the constructor for ProcessBuilder. The problem is that when the constructor is called it return null. Class code: public static void enable() throws IOException, InterruptedException { logger.info("Enable NTP server..."); String ntpAddress = AppConfig.getInstance().getString(AppConfig.NTP_SERVER, ""); AppConfig.getInstance().getBoolean(AppConfig.NTP_ENABLED, true); String enableNtp = "chkconfig ntpd on " + SEPARATOR + " service ntpd stop " + SEPARATOR + " ntpdate " +

Mocking a private constructor

牧云@^-^@ 提交于 2019-12-10 04:21:37
问题 The Site class is provided to me by an external team and has a private constructor. public class Site { int id; String brand; private Site(int id, String brand) { this.id = id; this.brand = brand; } } The SiteUtil class (controlled by team) is public class SiteUtil { public static Site getSite() { Site site; //Logic return site; } } The data the getSite() function applies it logic on requires a network call, therefore it needs to be mocked. It doesn't have a setter currently (maybe to

Cannot mock/spy final class using PowerMockito.spy()

亡梦爱人 提交于 2019-12-08 15:23:28
I'm trying to use PowerMockito to create a spy of a final class but I keep getting the following error, even though I am using PowerMockito's spy() method in place of Mockito's: java.lang.IllegalArgumentException: Cannot subclass final class class com.whoever.WidgetUploadClient My test case looks something like this: ... import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate(RobolectricTestRunner.class) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})

Cannot mock/spy final class using PowerMockito.spy()

≡放荡痞女 提交于 2019-12-08 06:38:35
问题 I'm trying to use PowerMockito to create a spy of a final class but I keep getting the following error, even though I am using PowerMockito's spy() method in place of Mockito's: java.lang.IllegalArgumentException: Cannot subclass final class class com.whoever.WidgetUploadClient My test case looks something like this: ... import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.spy; @RunWith(PowerMockRunner.class) @PowerMockRunnerDelegate

PowerMockito ClassNotPreparedException

萝らか妹 提交于 2019-12-08 04:18:24
I have the following jars: javax.servlet-api-3.1.9.jar junit-4.10.jar mockito-all-1.10.19.jar mockito-core-1.10.19.jar powermock-api-mockito-1.6.5.jar powermock-api-mockito-common-1.6.5.jar powermock-api-support-1.6.5.jar powermock-core-1.6.5.jar powermock-module-junit4-1.6.5.jar powermock-reflect-1.6.5.jar I want to test this method called createPanel which is inside a class called Controller : public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException { PanelManager.createPanel(req, res, hs); } In the ControllerTest class (junit tester)

Can not mock the private method with mockito

余生颓废 提交于 2019-12-08 03:49:48
问题 I am trying to mock a private method with power mockito, after reading this post I got some idea and I followed the same structure: example here is my class: public class test(){ private long verifyMarketEligibilityAndGetOfferDeliveryCalendar(long id) { some lins of code for connectiong to db } public long createOffer(long id){ return verifyMarketEligibilityAndGetOfferDeliveryCalendar(id); } } And here is my mock test: test classUnderTest = PowerMockito.spy(new test()); PowerMockito.doReturn

PowerMockito ClassNotPreparedException

拥有回忆 提交于 2019-12-08 03:33:00
问题 I have the following jars: javax.servlet-api-3.1.9.jar junit-4.10.jar mockito-all-1.10.19.jar mockito-core-1.10.19.jar powermock-api-mockito-1.6.5.jar powermock-api-mockito-common-1.6.5.jar powermock-api-support-1.6.5.jar powermock-core-1.6.5.jar powermock-module-junit4-1.6.5.jar powermock-reflect-1.6.5.jar I want to test this method called createPanel which is inside a class called Controller : public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs)

Unit test for Runnable with Mockito

淺唱寂寞╮ 提交于 2019-12-07 16:51:51
问题 I have a code like this for which I would like to write unit test. public class TestClass { private final Executor executor; private final Handler handler; TestClass(Executor executor, Handler handler) { this.executor = executor; this.handler = handler; } void doSomething(String param1) { executor.execute(new Runnable() { @Override public void run() { //do something handler.callHandler(); } }); } } How can I use Mockito / Powermockito to verify if the callHandler() method is invoked. 回答1:

PowerMockito mock single static method and return object inside another static method

ⅰ亾dé卋堺 提交于 2019-12-07 08:46:09
问题 I have written test cases to mock static classes and methods using PowerMockito's mockStatic feature. But I am strugling to mock one static method inside another static method. I did see few examples including this but none of them actually helping me or I am not understanding the actual functionality? (I'm clueless) Eg. I have a class as below and complete code is here. public static byte[] encrypt(File file, byte[] publicKey, boolean verify) throws Exception { //some logic here PGPPublicKey

Missing jacoco.exec file when using jacoco offline instrumentation with Powermock

末鹿安然 提交于 2019-12-06 11:47:31
问题 Despite apparently this post showed a solution to using powermock and jacoco, I haven't been able to make it work in a pretty simple project (available on GitHub). In my case, the test executes correctly but the jacoco.exec file is missing so jacoco doesn't check coverage. Test class: @RunWith(PowerMockRunner.class) @PrepareOnlyThisForTest(Util.class) @PowerMockIgnore("org.jacoco.agent.rt.*") public class UtilTest { @Test public void testSay() throws Exception { PowerMockito.mockStatic(Util