powermock

Mocking a static method with generic parameter

天大地大妈咪最大 提交于 2019-12-25 12:03:32
问题 i am trying to mock a static method with sigature public static <T extends Object> T get( String name, Class<T> i ) { return null } and i am using PowerMockito, and below is my expected code PowerMockito.when(ClassName.class, "get", "name", Class.class).thenReturn("Hi"); but its throwing org.powermock.reflect.exceptions.MethodNotFoundException: No method found with name 'get' with parameter types: [ java.lang.String, java.lang.Class ] Can anyone plz give the clue how to mock this using

How can I test two static methods (one invokes the other) with PowerMockito?

守給你的承諾、 提交于 2019-12-25 07:13:58
问题 How can I test the first static method method(String str, Param param) ? public class Example { public static Example method(String str, Param param) { return method(str, param, null); } private static Example method(String str, Param param, SomeClass obj) { // ... } } I try to test it with code below @Test public void should_invoke_overloaded_method() throws Exception { final String str = "someString"; mockStatic(Example.class); when(Example.method(str, paramMock)) .thenCallRealMethod();

Android + Powermock + Mockito + Maven build error with ClassNotFoundException

萝らか妹 提交于 2019-12-25 02:24:07
问题 I´m facing a strange build problem with powermock and mockito using maven. I can perfectly run the tests wihtin ecplise (wihtout using maven). But when i try to run the test with maven from cli or on the build server i get the following exception: Tests in error: Test mechanism: java.lang.ClassNotFoundException: org.mockito.internal.progres s.ThreadSafeMockingProgress My unit test looks like this: import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue

PowerMock + Mockito + Maven on Android app showing Dex loader error

折月煮酒 提交于 2019-12-24 17:43:35
问题 I'm trying to use PowerMock in my unit test (JUnit 4.12). I already have Mockito integrated without any issue. This is an Android application. When I run my unit test, no problem and the mocking of the static function works perfectly. When I click on the "play" button in eclipse to run my app on the physical machine connected, I receive this error: [2015-01-15 15:22:22 - Dex Loader] Unable to execute dex: Multiple dex files define Lorg/hamcrest/Description; [2015-01-15 15:22:22 - CLAP]

When will Javassist 3.17.0-GA be available [closed]

陌路散爱 提交于 2019-12-24 15:58:35
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Anyone know when Javassist 3.17.0-GA will be made available? There is a bug in Javaassit (fixed in 3.17.0-GA) that is breaking PowerMock when used with Java 7: https://issues.jboss.org/browse/JASSIST-160?focusedCommentId=12718716&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment

I am telling PowerMockito spy to return a value, so why is it calling the actual method?

有些话、适合烂在心里 提交于 2019-12-24 14:31:55
问题 I am trying to test some code using a PowerMockito spy, and I am stubbing a method (getRootTagMap--see below) to return a value constructed in the tester, (Using PowerMockito, because the method is private .) However instead of returning the value, it is always invoking the actual method, rather than returning the constructed value. Not sure what I am doing wrong import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import

“Unable to locate Spring NamespaceHandler” when using PowerMock

旧巷老猫 提交于 2019-12-24 13:16:03
问题 EDIT: When trying to create a minimal project that reproduces this issue to send in to the PowerMock folks, I was unable to reproduce the issue, so I'm not sure yet what other variable is causing the issue below. I'm trying to write a unit/integration test specifically to ensure our Spring bean configuration is written correctly. Looks something like this: public class TestSpringConfiguration { @Test public void loadsWithoutThrowing() { ClassPathXmlApplicationContext context = new

Powermock constructor mocking has no effect on the instantiated object

有些话、适合烂在心里 提交于 2019-12-24 11:47:10
问题 Class A{ B objB = new B(); objB.someBMethod(); } Class B{ public void someBMethof(){ C objC = new C(); } } class C{ int a=1; public C(){} public C(int v){ a=v; } } @RunWith( PoswerMockRunner.class ) @PrepareForTest({ A.class, B.class, C.class}) Class TestApp{ @Mock C mockC; PowerMockito.whenNew( C.class ).withNoArguments().thenReturn(mockC); } The above code captures what im trying to do. But the whenNew() does not seem to be working and when i try debuggin the C object created is not the

How to mock local variables in java? [duplicate]

给你一囗甜甜゛ 提交于 2019-12-24 02:14:23
问题 This question already has answers here : Mocking methods of local scope objects with Mockito (5 answers) Closed 6 years ago . In this situation? class A { public void f() { B b = new B(); C c = new C(); // use b and c, and how to modify their behaviour? } } How can I fullfill my idea with PowerMock and EasyMock ? I don't want to change my compact code for test reasons. 回答1: You can do this, see the answer by Matt Lachman. That approach isn't recommended however. It's a bit hacky. The best

mock static without @RunWith(PowerMockRunner.class)

微笑、不失礼 提交于 2019-12-23 12:53:09
问题 I have folowing code: @RunWith(PowerMockRunner.class) @PrepareForTest({RequestUtils.class, OsgiUtil.class}) @PowerMockIgnore({"*"}) public class MyTest ... @Test public somMethod(){ .... mockStatic(RequestUtils.class); when(RequestUtils.getLocale(request)).thenReturn(locale); } } How to replace this code so that it work without @RunWith(PowerMockRunner.class) ? According the cause described in following link I cannot use @RunWith(PowerMockRunner.class) 回答1: Take a look on this discussion: