powermockito

PowerMockito.doReturn returns null

試著忘記壹切 提交于 2019-12-02 10:36:46
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 failed: @RunWith(PowerMockRunner.class) @PowerMockIgnore("javax.crypto.*") public class TestA { //Spying

Wanted but not invoke: Mockito PrintWriter

只谈情不闲聊 提交于 2019-12-02 09:47:15
问题 Hi I am working on a project and using PrintWriter class for opening and writing in the file. But when I am writing the test case for same it gives following error at Line 153 Wanted but not invoked: mockPrintWriter.println("ID url1 "); -> at x.y.z.verify(ProcessImageDataTest.java:153) Actually, there were zero interactions with this mock. Code: (Uses Lombok Library) ProcessImageData.java @Setter @RequiredArgsConstructor public class ProcessImageData implements T { private final File

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

╄→гoц情女王★ 提交于 2019-12-02 05:30:25
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 have roughly the following Mockito-enhanced JUnit test: @Test public void testBar_callsBarWithXray() {

Wanted but not invoke: Mockito PrintWriter

戏子无情 提交于 2019-12-02 03:34:16
Hi I am working on a project and using PrintWriter class for opening and writing in the file. But when I am writing the test case for same it gives following error at Line 153 Wanted but not invoked: mockPrintWriter.println("ID url1 "); -> at x.y.z.verify(ProcessImageDataTest.java:153) Actually, there were zero interactions with this mock. Code: (Uses Lombok Library) ProcessImageData.java @Setter @RequiredArgsConstructor public class ProcessImageData implements T { private final File newImageDataTextFile; @Override public void execute() { LineIterator inputFileIterator = null; try { File

PowerMock + Emma - code coverage showing 0% for private static methods and other methods too [duplicate]

好久不见. 提交于 2019-12-01 13:27:51
This question already has an answer here: PowerMock ECLEmma coverage issue 7 answers I have taken a reference of PowerMock from : Mock private method using PowerMockito and applied the same logic here. Also, I installed EMMA (open source tool) in eclipse/STS, but when I run the code I see zero % code coverage. why ? public class MyClient { public void publicApi() { System.out.println("In publicApi"); int result = 0; try { result = privateApi("hello", 1); } catch (Exception e) { //Assert.fail(); } System.out.println("result : "+result); if (result == 20) { throw new RuntimeException("boom"); }

How to mock private methods using Whitebox(org.powermock.reflect)

久未见 提交于 2019-12-01 10:46:41
I want to mock a private method which has been called inside another method. Following is the sample code, I have written. Java code: package org.mockprivatemethods; public class AccountDeposit { // Instantiation of AccountDetails using some DI AccountDetails accountDetails; public long deposit(long accountNum, long amountDeposited){ long amount = 0; try{ amount = accountDetails.getAmount(accountNum); updateAccount(accountNum, amountDeposited); amount= amount + amountDeposited; } catch(Exception e){ // log exception } return amount; } private void updateAccount(long accountNum, long

How to mock private methods using Whitebox(org.powermock.reflect)

穿精又带淫゛_ 提交于 2019-12-01 08:50:21
问题 I want to mock a private method which has been called inside another method. Following is the sample code, I have written. Java code: package org.mockprivatemethods; public class AccountDeposit { // Instantiation of AccountDetails using some DI AccountDetails accountDetails; public long deposit(long accountNum, long amountDeposited){ long amount = 0; try{ amount = accountDetails.getAmount(accountNum); updateAccount(accountNum, amountDeposited); amount= amount + amountDeposited; } catch

How can I mock an instance of an enum class with PowerMock & Mockito?

谁说胖子不能爱 提交于 2019-12-01 05:02:28
I tried to follow the example offered in the answer to this very similar question, but it does not work for me. I get the following error message: java.lang.IllegalArgumentException: Cannot subclass final class class com.myproject.test.support.ExampleEnumerable at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito

PowerMock & Java 11

萝らか妹 提交于 2019-12-01 03:53:52
We are using PowerMock in few of our historical projects. Unfortunately PowerMock is quite dead and is not compatible with Java 11. And we are using mockStatic(). Yes, we know its considered harmful - its in the legacy code and we would prefer not to rewrite those classes now... Are there any options how to tweak PowerMock to support Java 11? Or is it possible to easily replace it with some other Java 11 compatible framework? (Mockito does not support mockStatic) After one year of no releases, things are really moving in PowerMock. PowerMock 2.0.0-RC1 was released. And with PowerMockito 2.0.0

PowerMock & Java 11

吃可爱长大的小学妹 提交于 2019-12-01 00:54:43
问题 We are using PowerMock in few of our historical projects. Unfortunately PowerMock is quite dead and is not compatible with Java 11. And we are using mockStatic(). Yes, we know its considered harmful - its in the legacy code and we would prefer not to rewrite those classes now... Are there any options how to tweak PowerMock to support Java 11? Or is it possible to easily replace it with some other Java 11 compatible framework? (Mockito does not support mockStatic) 回答1: After one year of no