powermock

PowerMock - IllegalStateException: Must replay Class XXX to get configured expectation

眉间皱痕 提交于 2019-12-07 19:27:00
问题 @PrepareForTest({...,..., XXX.class}) ... @Test public void testMethodToBeTested(){ XXX mockInstance = PowerMock.createMock(XXX.class); ... PowerMock.expectNew(XXX.class, p1, p2, p3, p4, p5).andReturn(mockInstance); mockInstance.methodWithNoReturnVal(); expect(mockInstance.getSearchVal()).andReturn(1); PowerMock.replay(mockInstance); Whitebox.invokeMethod(objInstance, "methodToBeTested"); PowerMock.verify(mockInstance); } Essentially, I want to test a private method: methodToBeTested(). This

Is it possible to mock a single method in an already existing object?

感情迁移 提交于 2019-12-07 13:33:21
问题 For an integration test, I need to mock a specific method in a java service client without destroying the rest of the information in it. It has no self-constructor, so a solution like this is out of the question: private DBClient mockClient = new DBClient(alreadyExistingClient){ @Override void deleteItem(Item i){ //my stuff goes here } }; Is there a way to mock the deleteItem method such that the credentials, endpoints, etc... are preserved in an existing DBClient object? edit: mockito is not

SuppressStaticInitializationFor(Powermock)

我怕爱的太早我们不能终老 提交于 2019-12-07 13:15:28
I have a public class that has static variables, static blocks and static functions. I am testing one of the static function(say x) from my tester class , I have suppressed static block by using @SuppressStaticInitializationFor at the class level(Powermock) in the tester class . Now when I am running Jnunit test from my tester class I am getting null pointer exception when control reaches the above function that is using the static variables of the class.So my question is that does @SuppressStaticInitializationFor supress initialization of static variables too in the class or is it just

PowerMockito - How do I use whenNew() with a typed List?

走远了吗. 提交于 2019-12-07 12:36:55
问题 I want PowerMockito to return my empty array-list of Foo s when new ArrayList<Foo>() is called, but I am not sure how to construct the statement. Specifically, I want new ArrayList<AnyOtherType>() to create a new list as normal. ArrayList<Foo> fooList = new ArrayList<Foo>(); PowerMockito.whenNew(ArrayList.class).withParameterTypes(Foo.class).thenReturn(fooList); ^ Here's basically what I have, but .withParameterTypes(Foo.class) does not allow me to follow with a .thenReturn() . My only option

Testing Spring controllers using Powermock

强颜欢笑 提交于 2019-12-07 10:31:51
问题 I have a class which tests specific controller and it works fine @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:config/test-applicationContext-config.xml") @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class TestAdminController { //..... } I have used there Google's Mockito library from mockito.org to mock my underlying beans. Now my

Unable to mock static methods in instrumentation tests

£可爱£侵袭症+ 提交于 2019-12-07 08:42:37
问题 I am having a hard time in mocking static methods for instrumentation ( Espresso ) tests. For mocking objects, I am using Mockito . But, since Mockito cannot mock static methods , I am using Powermock on top of it. This works fine for tests running on the JVM machine, but for UI Tests, this combination does not work fine. I have declared the following dependencies for instrumentation tests. androidTestCompile 'org.mockito:mockito-core:1.10.19' androidTestCompile 'org.powermock:powermock-api

PowerMockito Mocking whenNew not taking effect

狂风中的少年 提交于 2019-12-07 08:27:13
问题 Description: I cannot seem to have my stubs or mocks take affect in the class I have under test. I am trying to use the whenNew action so I can mock a return object and then mock a operation on that object with a returned value. I imagine its something simple I am missing but not seeing it. SOLUTION: Originally I was running with MockitoRunner.class and it required being changed to PowerMockRunner.class . Code below reflects the solution. Jars on the classpath: powermock-mockito-1.4.11-full

Runtime error PowerMock + Mockito: ProxyFrameworkImpl could not be located in classpath

核能气质少年 提交于 2019-12-07 06:26:59
问题 I am trying to use PowerMock with the Android InstrumentTestCase Since my test runs on an Android device the libraries needs to be added to the apk. I encounter big issues with powermock+mockito and Dex files. I have a runtime error with only powermock+mockito in my dependencies: org.powermock.api.extension.proxyframework.ProxyFrameworkImpl could not be located in classpath. And a compilation error if I include either cglib/cglib-nodep (has suggested in answers): com.android.dex.DexException:

Using PowerMock to mock static class in a rest-assured test

为君一笑 提交于 2019-12-07 06:09:18
问题 Is there any way to use PowerMock with rest-assured, because when I'm trying to test a RESTful API with rest-assured. I want to PowerMock a static call. The code of operation: @POST @Produces("application/json") @Consumes(MediaType.APPLICATION_JSON) public Response createEntity(@Context HttpHeaders hh, String body) { . . . String sec = MDI.check(Some_string, ..., ...); if (sec == null) throw ... return Response.... } And the test: @RunWith(PowerMockRunner.class) @PrepareForTest(MDI.class)

How can I initialize kafka ConsmerRecords<String,String> in kafka for testing

无人久伴 提交于 2019-12-07 04:13:25
问题 I am writing test cases for kafka consumer components and mocking kafkaConsumer.poll() which returns instance of ConsumerRecords<String,String> . I want to initialize ConsumerRecords and use that in mock but the constructors of ConsumerRecords expect actual kafka topic which I don't have in tests. One way I think for this is by keeping a serialized copy of object and deserialize to initialize ConsumerRecords . Is there any other way to achieve the same. 回答1: Here is some example code (Kafka