powermockito

ClassCastException exception when running Robolectric test with Power Mock on multiple files

拜拜、爱过 提交于 2019-11-30 12:46:46
So I set up the power mock based on the reference guide here . It all seems to run perfectly fine with a single test class. But when executing multiple JUnit tests I am getting the following error on the second test class. As you can see from the stacktrace below I am trying to mock a otto Bus instance. It seemed to mock properly on the first test class but on the the second class I am getting this class cast exception. On the stacktrace I am getting suggestion to disable Objenisis cache but I am not sure how to accomplish that and if that is actually the root cause as I am using classloading

ClassCastException exception when running Robolectric test with Power Mock on multiple files

僤鯓⒐⒋嵵緔 提交于 2019-11-29 17:59:05
问题 So I set up the power mock based on the reference guide here. It all seems to run perfectly fine with a single test class. But when executing multiple JUnit tests I am getting the following error on the second test class. As you can see from the stacktrace below I am trying to mock a otto Bus instance. It seemed to mock properly on the first test class but on the the second class I am getting this class cast exception. On the stacktrace I am getting suggestion to disable Objenisis cache but I

powermockito: How to mock abstract method in enum

流过昼夜 提交于 2019-11-29 15:43:38
Consider the following (simplified) enumeration: MyEnum { ONE public int myMethod() { // Some complex stuff return 1; }, TWO public int myMethod() { // Some complex stuff return 2; }; public abstract int myMethod(); } This is used in a function like: void consumer() { for (MyEnum n : MyEnum.values()) { n.myMethod(); } } I'd now like to write a unit test for consumer that mocks out the calls to myMethod() in each of the enumeration instances. I've tried the following: @RunWith(PowerMockRunner.class) @PrepareForTest(MyEnum.class) public class MyTestClass { @Test public void test() throws

Mock static method with GroovyMock or similar in Spock

我与影子孤独终老i 提交于 2019-11-28 12:00:25
First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background, I'm in the process of retrofitting tests in legacy java. Refactoring is prohibited. I'm using spock-0.7 with groovy-1.8. The call to the static method is chained with an instance call in this form: public class ClassUnderTest{ public void methodUnderTest(Parameter param){ //everything else commented out Thing someThing = ClassWithStatic

How to mock a Kotlin singleton object?

感情迁移 提交于 2019-11-28 09:42:20
Given a Kotlin singleton object and a fun that call it's method object SomeObject { fun someFun() {} } fun callerFun() { SomeObject.someFun() } Is there a way to mock call to SomeObject.someFun() ? Just make you object implement an interface, than you can mock you object with any mocking library. Here example of Junit + Mockito + Mockito-Kotlin : import com.nhaarman.mockito_kotlin.mock import com.nhaarman.mockito_kotlin.whenever import org.junit.Assert.assertEquals import org.junit.Test object SomeObject : SomeInterface { override fun someFun():String { return "" } } interface SomeInterface {

powermockito: How to mock abstract method in enum

孤街浪徒 提交于 2019-11-28 09:24:32
问题 Consider the following (simplified) enumeration: MyEnum { ONE public int myMethod() { // Some complex stuff return 1; }, TWO public int myMethod() { // Some complex stuff return 2; }; public abstract int myMethod(); } This is used in a function like: void consumer() { for (MyEnum n : MyEnum.values()) { n.myMethod(); } } I'd now like to write a unit test for consumer that mocks out the calls to myMethod() in each of the enumeration instances. I've tried the following: @RunWith(PowerMockRunner

Verify Static Method Call using PowerMockito 1.6

泪湿孤枕 提交于 2019-11-27 21:19:45
问题 I am writing JUnit test case for methods similar to sample given below: Class SampleA{ public static void methodA(){ boolean isSuccessful = methodB(); if(isSuccessful){ SampleB.methodC(); } } public static boolean methodB(){ //some logic return true; } } Class SampleB{ public static void methodC(){ return; } } I wrote the following test case in my test class: @Test public void testMethodA_1(){ PowerMockito.mockStatic(SampleA.class,SampleB.class); PowerMockito.when(SampleA.methodB())

Mock static method with GroovyMock or similar in Spock

…衆ロ難τιáo~ 提交于 2019-11-27 03:53:53
问题 First-timer here, apologies if I've missed anything. I'm hoping to get around a call to a static method using Spock. Feedback would be great With groovy mocks, I thought I'd be able to get past the static call but haven't found it. For background, I'm in the process of retrofitting tests in legacy java. Refactoring is prohibited. I'm using spock-0.7 with groovy-1.8. The call to the static method is chained with an instance call in this form: public class ClassUnderTest{ public void

Mockito: Mock private field initialization

北城以北 提交于 2019-11-27 01:00:08
How I can mock a field variable which is being initialized inline? e.g. class Test { private Person person = new Person(); ... public void testMethod() { person.someMethod(); ... } } Here I want to mock person.someMethod() while testing method - Test#testMethod. for which I need to mock initialization of person variable. Any clue? EDIT: I'm not allowed to modify Person class. Mockito comes with a helper class to save you some reflection boiler plate code: import org.mockito.internal.util.reflection.Whitebox; //... @Mock private Person mockedPerson; private Test underTest; // ... @Test public

How to mock a Kotlin singleton object?

陌路散爱 提交于 2019-11-26 18:25:29
问题 Given a Kotlin singleton object and a fun that call it's method object SomeObject { fun someFun() {} } fun callerFun() { SomeObject.someFun() } Is there a way to mock call to SomeObject.someFun() ? 回答1: Just make you object implement an interface, than you can mock you object with any mocking library. Here example of Junit + Mockito + Mockito-Kotlin: import com.nhaarman.mockito_kotlin.mock import com.nhaarman.mockito_kotlin.whenever import org.junit.Assert.assertEquals import org.junit.Test