powermockito

Error trying to mock constructor for ProcessBuilder using PowerMockito

不打扰是莪最后的温柔 提交于 2019-12-06 05:08:54
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 " + ntpAddress + " " + SEPARATOR + " service ntpd start"; String[] commandArr = {"bash", "-c", enableNtp};

PowerMockito.verifyStatic() Problems

与世无争的帅哥 提交于 2019-12-05 22:01:35
问题 I need to use PowerMockito to test if a specific static method is called. I am using the following PowerMockito and JUnit libraries ... powermock-mockito-1.6.2-full.jar junit-4.12.jar I am having issues getting the PowerMockito.verifyStatic() method to work properly. In the following code example, I have tried using the @PrepareForTest, and mockStatic(), and I have tried excluding them. In the code example I include them. Test Class: import org.powermock.api.mockito.PowerMockito; import org

Unit test for Runnable with Mockito

♀尐吖头ヾ 提交于 2019-12-05 21:41:59
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. Pass a mock Handler to the constructor of TestClass . Then use Mockito.verify() to assert that callHandler()

PowerMockito .when().thenReturn() with randomUUID not returning expected value [duplicate]

半城伤御伤魂 提交于 2019-12-05 16:22:45
This question already has answers here : How do I unit test code which uses Java UUID? (4 answers) Closed 2 years ago . I'm trying to test a Web Service method which connects to a SQL Server Database which contains JCR nodes, as we're using JackRabbit. The method looks like: public String addDocumentByJson(String fileName, byte[] fileContent, int status, String userName, String jsonProperties) { UUID id = UUID.randomUUID(); // It does a bunch of operations here return jsonResult; } Where jsonResult is an object similar to this one: { "id" : "<A random UUID>" "version" : 1 } Now, when I try to

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

时光怂恿深爱的人放手 提交于 2019-12-05 15:17:54
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 encryptionKey = OpenPgpUtility.readPublicKey(new ByteArrayInputStream(publicKey)); //some other logic

How to mock constructor with PowerMockito

会有一股神秘感。 提交于 2019-12-05 07:18:55
I'm trying to mock a class constructor with PowerMockito for first time, but it doesn't work. My current code is: public class Bar { public String getText() { return "Fail"; } } public class Foo { public String getValue(){ Bar bar= new Bar(); return bar.getText(); } } @RunWith(PowerMockRunner.class) @PrepareForTest(Bar.class) public class FooTest { private Foo foo; @Mock private Bar mockBar; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); PowerMockito.whenNew(Bar.class).withNoArguments().thenReturn(mockBar); foo= new Foo(); } @Test public void testGetValue()

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

你说的曾经没有我的故事 提交于 2019-12-04 16:59:55
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.class); Mockito.when(Util.say(Mockito.anyString())).thenReturn("hello:mandy"); Assert.assertEquals(

PowerMockito.doReturn returns null

元气小坏坏 提交于 2019-12-04 06:22:14
问题 This is my class under test: public class A { public Integer callMethod(){ return someMethod(); } private Integer someMethod(){ //Some Code HttpPost httpPost = new HttpPost(oAuthMessage.URL); //Some Code HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpPost); ------1 Integer code = httpResponse.getStatusLine().getStatusCode(); ---2 return code; } Now I want to mock the line 1 & 2 & return a mock HttpResponse & code. I have tried this but

How do I verify the number of invocations of overloaded method using Mockito?

做~自己de王妃 提交于 2019-12-04 05:02:44
问题 How do I check if bar(Alpha, Baz) called bar(Xray, Baz) using Mockito - without actually calling the later, given my MCVE class Foo : public class Foo { public String bar(Xray xray, Baz baz) { return "Xray"; } public String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get() instanceof Xray) { return bar((Xray)alpha.get(), baz); } else if(alpha.get() instanceof Zulu) { return bar((Zulu)alpha.get(), baz); } else { return null; } } } Currently, I

How do I return different values on different calls to a mock?

痞子三分冷 提交于 2019-12-04 04:49:37
I have the following code which is getting the current counter value from DB. Then it updates the counter in DB and then again it retrieves the value. int current = DBUtil.getCurrentCount(); DBUtil.updateCount(50);// it updates the current count by adding 50 int latest = DBUtil.getCurrentCount(); I want to mock the static methods in such a way that the first call should return 100 and the second call should return 150. How can I use PowerMockito to achieve this? I am using TestNG, Mockito along with PowerMock. durron597 Mockito supports changing the returned value; this support extends to