powermock

PowerMockito Mocking whenNew not taking effect

亡梦爱人 提交于 2019-12-05 14:56:08
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.jar mockoito-all-1.9.0.jar javassist-3.15.0-GA.jar junit-4.8.2.jaf objensis-1.2.jar cglib-nodep-2.2.2

Powermock mockstatic Cannot subclass final class

我们两清 提交于 2019-12-05 13:51:31
问题 I am trying to mock a final class PowerMockito.mockStatic(TestFinalClass.class); It is working from my eclipse when I run a single junit and add javaagent to my VM arguments -javaagent:{path}/powermock-module-javaagent-1.6.4.jar But when I try to run all test cases from command line using maven build command I am still getting "Cannot subclass final class" Below is my snippet from pom.xml <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId>

Mockito / PowerMocktio doNothing for none void method

别说谁变了你拦得住时间么 提交于 2019-12-05 11:47:47
问题 I need a method which returns something to do nothing when invoked during testing, the class instance which owns the method is implemented as a spy. I am aware the doNothing() method only works with void methods. Is there a way to get the same behaviour with a method that returns something? Thank you! 回答1: Use when(spy.myMethod()).thenReturn(null) . This will prevent the spy from invoking the wrapped instance. You have to tell Mockito what to return for a method that returns something. The

Powermock JUnit tests are taking more time to execute compared to normal JUnit

試著忘記壹切 提交于 2019-12-05 11:34:07
we are using powermock for mocking the static methods by using the @PrepareForTest annotations. The test runs fine but the problem is it is taking more time to execute the test. The code snippet is like below: @PrepareForTest({ StaticClass1.class, StaticClass2.class }) Normally, the JUnit with out mocking the static method is taking around 2 sec time to execute but when we add @PrepareForTest annotation for mocking the static calls, the test is taking around a minute time to complete the execution. For one test, this is not a big impact, but we have more than 1K tests and the overall build

apkbuilder finds duplicate file when adding powermock to an android test project

余生颓废 提交于 2019-12-05 11:24:24
I'm attempting to add powermock-mockito and mockito into an Android Test project. I created the android test project using the android command-line tool to create the build.xml and project structure. I have the following jars in my libs/ directory: dexmaker-1.0.jar dexmaker-mockito-1.0.jar mockito-all-1.9.5.jar powermock-mockito-1.5-full.jar When I attempt to build the project with ant debug, I get the following error: [apkbuilder] Creating ProjectTests-debug-unaligned.apk and signing it with a debug key... [apkbuilder] Found duplicate file for APK: mockito-extensions/org.mockito.plugins

Mock java.lang.Runtime with PowerMockito

删除回忆录丶 提交于 2019-12-05 11:20:01
want to write an unittest for a method like public static void startProgram() { process = Runtime.getRuntime().exec(command, null, file); } I don't want to inject the runtime object for some reasons, so I wanted to stub the getRuntime method that it returns a Runtime mock... I tried it this way: @RunWith(PowerMockRunner.class) @PrepareForTest(Runtime.class) public class ProgramTest { @Test public void testStartProgram() { Runtime mockedRuntime = PowerMockito.mock(Runtime.class); PowerMockito.mockStatic(Runtime.class); Mockito.when(Runtime.getRuntime()).thenReturn(mockedRuntime); ... //test } }

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

时光毁灭记忆、已成空白 提交于 2019-12-05 11:02:28
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. Here is some example code (Kafka clients lib version 0.10.1.1): import java.util.ArrayList; import java.util.Collection; import java.util

@PowerMockIgnore at project level

北城以北 提交于 2019-12-05 08:01:50
I have following error in my powermock test cases while running in Maven : java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name "javax/management/MBeanServer" The solution is to add annotation @PowerMockIgnore("javax.management.*") The problem is I have many test files where I have to add this annotation. Is there a way to add this at project level or in maven? Thanks I don't think that this is possible. Keep in mind, in the end, it is JUnit that is executing those

Mocking a final method with PowerMock + EasyMock

↘锁芯ラ 提交于 2019-12-05 06:48:21
I'm trying to mock a call to the final method ResourceBundle.getString() . With PowerMock 1.4.12 and EasyMock 3.1, the call is not being mocked; instead, the "real" method is called. My test class: @RunWith(PowerMockRunner.class) @PrepareForTest(ResourceBundle.class) public class TestSuite { @Before public void setUp() throws Exception { ResourceBundle resourceBundleMock = PowerMock.createNiceMock(ResourceBundle.class); expect(resourceBundleMock.getString(BundleConstants.QUEUE)).andReturn("Queue"); PowerMock.replay(resourceBundleMock); beanBeingTested.setMessages(resourceBundleMock); } ... }