powermockito

PowerMockito verify that a static method is never called

懵懂的女人 提交于 2020-03-03 07:03:28
问题 I am writing a JUnit test to verify that a static method ( MyClass.myMethod() ) is never invoked in the method flow. I tried doing something like this: PowerMockito.verifyStatic(Mockito.never()); MyClass.myMethod(Mockito.any()); In doing so I receive an UnfinisedVerificationException. How do I test that MyClass.class has no interactions whatsoever in the method execution? 回答1: UnfinishedVerificationException will occur if the Class is not mocked yet but you are trying to verify the invocation

How to mock getResourceAsStream method using PowerMockito and JUnit?

断了今生、忘了曾经 提交于 2020-01-24 10:11:05
问题 I try to mock getResourceAsStream method which I invoke in constructor. public Conn() { stream = Conn.class.getClass().getResourceAsStream(PATH); } For mock framework I prefer Mockito + PowerMockito. @RunWith(PowerMockRunner.class) @PrepareForTest(Conn.class) public class ConnTest { @Mock private InputStream streamMock; private Conn conn; @Before public void before() { initMocks(this); } @Test public void test() { PowerMockito.mockStatic(Conn.class); PowerMockito.when(Connector.class

Realm Unit Testing

若如初见. 提交于 2020-01-22 20:03:07
问题 I am trying to unit test Realm and its interactions but things are not going too well. I have included all dependencies and keep getting vague failures, below is my code for the Helper class which is a wrapper over Realm . Questions Is this the correct way of testing Realm? How can I test data that is in the app's sandbox, can that data only be tested by UI/Instrumentation tests? I am getting an error currently (below) and before I was getting a "Powermock zero args constructor doesn't exist"

UnfinishedStubbingException when working with doNothing method for void methods

限于喜欢 提交于 2020-01-16 19:05:38
问题 The below code is causing UnfinishedStubbingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString()); verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update1"), Matchers.eq(jsonObject), anyString()); org.mockito.exceptions.misusing

UnfinishedStubbingException when working with doNothing method for void methods

荒凉一梦 提交于 2020-01-16 19:04:09
问题 The below code is causing UnfinishedStubbingException PowerMockito.doNothing().when(widgetHelper).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update"), Matchers.eq(jsonObject), anyString()); verify(widgetHelper, times(1)).invokeAuditService(Matchers.eq(servletRequest), Matchers.eq(date), anyString(), Matchers.eq("Member_Servicing_Email_Update1"), Matchers.eq(jsonObject), anyString()); org.mockito.exceptions.misusing

PowerMock + Robolectric + Dagger2. Part I

荒凉一梦 提交于 2020-01-13 05:14:53
问题 This question was created from first part of PowerMock + Robolectric + Dagger2 So i'm a little bit again. Sorry. I test custom view class which contain: android ui elements some logic static methods callings dagger2 dependencies So i use next tools for testing Robolectric for UI elements mocking unit tests for logic testing PowerMock for static methods mocking Robolectric + PowerMock integration problem is known and solution is known - https://github.com/robolectric/robolectric/wiki/Using

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

醉酒当歌 提交于 2020-01-12 17:28:33
问题 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

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

我的梦境 提交于 2020-01-12 17:28:30
问题 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

Mocking execte() method of HttpClient [duplicate]

爷,独闯天下 提交于 2020-01-06 05:51:08
问题 This question already has an answer here : Mocked HttpClient calls actual method (1 answer) Closed 5 months ago . I want to mock execute method of HttpClass to return a dummy response, but my test code is acutally executing the request. Class Code - class MyService { private CloseableHttpClient newHttpClient() { try { SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( new SSLContextBuilder().loadTrustMaterial(TrustAllStrategy.INSTANCE).build(), NoopHostnameVerifier.INSTANCE);

Can I chain returns with PowerMockito using doReturn?

给你一囗甜甜゛ 提交于 2020-01-06 03:34:08
问题 I have a method call that needs to return valueA the first time it is called and valueB the second time it is called. I'm using a PowerMockito spy, so if I were just needing to return one value it would look like this: PowerMockito.doReturn(valueA).when(mockedObject, "methodName"); It looks like I can do chained returns like this: PowerMockito.when(mockedObject, "methodName").thenReturn(valueA).thenReturn(valueB); But I need to indicate chained returns with doReturn so that the real