powermock

PowerMock will not work with JAXB Unmarshal

a 夏天 提交于 2019-12-10 13:15:29
问题 I'm creating a test case wherein, I input xml and unmarshal it to procced with processing. I'm trying to use PowerMock and I keep getting javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.xxxxxxx.org/xxxxx/xx/xx", local:"Element"). Expected elements are <{}NotifRQ>,<{http://www.xxxxxxx.org/xxxxx/xx/xx}NotifRS>,etc at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:659) at com.sun.xml.internal.bind.v2.runtime

org.mockito.exceptions.misusing.UnfinishedStubbingException Unfinished stubbing detected

霸气de小男生 提交于 2019-12-10 11:26:01
问题 I have wrote following code: @RunWith(PowerMockRunner.class) @PrepareForTest(Integer.class) public class TestClass{ @Test public void test(){ PowerMockito.mockStatic(Integer.class); when(Integer.parseInt(anyString())).thenReturn(0); System.out.println(Integer.parseInt("12")); } } I got following error message : org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at com.ctc.dime.services.autopublisher.stores.StoresPublishingServiceTest.test

use mockito to stub final method [duplicate]

*爱你&永不变心* 提交于 2019-12-10 11:26:00
问题 This question already has answers here : Final method mocking (7 answers) Closed 5 years ago . I need to use a mock who has a final method. So i use powermock but it does not work class B { public final int nb() { return 4; } } @RunWith(PowerMockRunner.class) @PrepareForTest(B.class) public class Exemple extends TestCase { @Test public void test() { B b = PowerMockito.mock(B.class); PowerMockito.when(b.nb()).thenReturn(5); final int actualState = b.nb(); assertEquals(5, actualState); } } if

How can I load a json file from an android unit test that is run with PowerMockRunner?

泪湿孤枕 提交于 2019-12-10 10:30:08
问题 I am using PowerMockRunner to run my unit tests. I want to load some canned network response json files from my assets folder. I am using this method to try to get the file. private static File getFileFromPath(Object obj, String fileName) { ClassLoader classLoader = obj.getClass().getClassLoader(); URL resource = classLoader.getResource(fileName); return new File(resource.getPath()); } I call the method like this from my class which has these annotations at the top. @RunWith(PowerMockRunner

@PowerMockIgnore at project level

泄露秘密 提交于 2019-12-10 04:35:49
问题 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?

mock测试之powermock

半城伤御伤魂 提交于 2019-12-10 02:40:45
由于公司框架依赖别的模块, 导致我们开发老是需要跟着他们的脚步, 所以我的上级领导提出这个方案说直接跳过他们,我们自己在本地测试,然后就找了它, 导入相关jar <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.8.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.4.12</version> <scope>test</scope> </dependency> PowerMock 介绍与使用 一、为什么要使用Mock工具 在做单元测试的时候,我们会发现我们要测试的方法会引用很多外部依赖的对象,比如:

PowerMock, mockito, verify static method

懵懂的女人 提交于 2019-12-10 01:47:47
问题 I'm trying to get PowerMock to work with mockito, and I'm following the documentation here: http://code.google.com/p/powermock/wiki/MockitoUsage13. To simplify a bit, lets say that I have a static method: StaticObj.put(String key, String val) { ... } And the class to be tested does something like this: public class ClassToTest { public void doSomething(Params p) { if (StringUtils.isNotBlank(p.getK()) StaticObj.put("k1", p.getK()); if (StringUtils.isNotBlank(p.getX()) StaticObj.put("x1", p

Mockito asks to add @PrepareForTest for the class even after adding @PrepareForTest

喜夏-厌秋 提交于 2019-12-09 14:30:50
问题 I have the following simple code. I have a class (TestClass) and I want to test "someMethod". There is an external static method which is called by my "someMethod". I want to Powermock that static method to return me some dummy object. I have the @PrepareForTest(ExternalClass.class) in the begining, but when I execute it gives the error: The class ExternalClass not prepared for test. To prepare this class, add class to the '@PrepareForTest' annotation. In case if you don't use this annotation

Bad type on the operand stack in arraylength

五迷三道 提交于 2019-12-09 08:08:55
问题 I am trying to test a method using powermock. i haven't written any test cases yet. Just trying to set up the class for Mocking. here is what I have so far: @RunWith(PowerMockRunner.class) @PrepareForTest({ ReadRubric.class }) public class ReadRubricTest { @Before public void setUp() throws Exception { PowerMockito.mock(ReadRubric.class); } @After public void tearDown() throws Exception { } @Test public void invalidFile() throws Exception { } } When I try to run this test I get the following

Powermock mocking void method throws error

纵饮孤独 提交于 2019-12-09 03:56:23
问题 I am using Powermockito, mockito with TestNG. My test class extends PowerMockTestCase. I want to mock a void method. For that I used following sample syntax, @PrepareForTest(TestClass.class) class Sample extends PowerMockTestCase{ @BeforeClass public void beforeClass(){ TestClass obj = PowerMockito.spy(TestClass.getInstance()); PowerMockito.doNothing().when(obj).voidMethodName(Matchers.any(Type1.class), Matchers.anyString(), Matchers.any(Type2.class)); } When I give this, I get : FAILED