powermockito

PowerMockito: NotAMockException on a mock

房东的猫 提交于 2019-12-21 03:45:41
问题 Bit of a complicated setup. Robolectric, PowerMockito rule-based config. @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) @PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) // Using "PrepareOnlyThis" prevents powermock from trying to instrument the whole hierarchy, // part of which we've ignored (android.os.* in this case) @PrepareOnlyThisForTest({ServiceCallbackBase.class}) // this class extends Handler, // so we need

PowerMockito throws NullPointerException when trying to stub private overloaded method

*爱你&永不变心* 提交于 2019-12-20 03:25:17
问题 I'm (still) trying to check if bar(Alpha, Baz) called bar(Xray, Baz) using PowerMockito (as bar(Xray, Baz) is private ) - without actually calling the later, given my MCVE class Foo below. (I went through the same class earlier, with all methods in Foo being public - in case you've got a déjà vu...) public class Foo { private String bar(Xray xray, Baz baz) { return "Xray"; } private String bar(Zulu zulu, Baz baz) { return "Zulu"; } public String bar(Alpha alpha, Baz baz) { if(alpha.get()

How do I mock a private field using PowerMockito? [duplicate]

老子叫甜甜 提交于 2019-12-19 12:01:41
问题 This question already has answers here : How do I test a private function or a class that has private methods, fields or inner classes? (51 answers) Closed last year . The solution proposed as a duplicate is not a PowerMockito solution and as a consequence does not answer this question. Further, this question IS answered legitimately below. IDK if this is a duplicate or not but I sure can't find the item in question if it is. I keep expecting this to be really simple, since it is pretty

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

梦想与她 提交于 2019-12-19 11:17:25
问题 This question already has answers here : PowerMock ECLEmma coverage issue (7 answers) Closed last year . 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

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

烂漫一生 提交于 2019-12-19 11:16:07
问题 This question already has answers here : PowerMock ECLEmma coverage issue (7 answers) Closed last year . 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

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

好久不见. 提交于 2019-12-19 06:46:08
问题 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

Mockito: Mock private field initialization

假如想象 提交于 2019-12-17 06:29:08
问题 How I can mock a field variable which is being initialized inline? e.g. class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock person.someMethod() while testing method - Test#testMethod. for which I need to mock initialization of person variable. Any clue? EDIT: I'm not allowed to modify Person class. 回答1: Mockito comes with a helper class to save you some reflection boiler plate code: import org.mockito.internal.util

PowerMock java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection

人盡茶涼 提交于 2019-12-13 13:21:29
问题 I created a mock HttpsURLConnection based on an StackExchange answer: import java.net.URL; import javax.net.ssl.HttpsURLConnection; ... @RunWith(PowerMockRunner.class) public class DialogTest { public void mockHttpsUrlConnectionExample() throws Exception { URL mockUrl = PowerMockito.mock(URL.class); PowerMockito.whenNew(URL.class).withAnyArguments().thenReturn(mockUrl); HttpsURLConnection mockUrlConnection = PowerMockito.mock(HttpsURLConnection.class); PowerMockito.when(mockUrl.openConnection

How to test a void function with sequential calls

╄→尐↘猪︶ㄣ 提交于 2019-12-13 08:23:10
问题 How can one test a function that does not return a value, but uses given arguments to construct some values which are being sent to a sequential function? For example public void handleSomeEvent(String str1, String str2){ MyObject obj1 = new MyObject(); obj1.setParameterA(str1); obj2.setParameterB(str2); EventHandler.getInstance().notify(obj1) } In the above example i would like to verify EventHandler.notify was called with an object containing str1 as parameterA and str2 as parameter2 . Can

How to mock new instance of RestTemplate?

穿精又带淫゛_ 提交于 2019-12-13 03:25:34
问题 Im writing a unit test for my legacy code which is something like: public class SomeClass { public SomeResponse logToServer() { SomeResponse response = null; try{ RestTemplate restTemplate = new RestTemplate(); SomeRequestBean request = new SomeRequestBean(); response = restTemplate.postForEntity("http://someUrl", request, SomeResponse.class); System.out.println(response.toString()); } catch(Exception e) { e.printStackTrace(); } return response; } } I know I can move RestTemplate above and