How to mock an array of interfaces using powermock or mockito -


i mocking interface array throws java.lang.illegalargumentexception: cannot subclass final class class.

following changes did.

added following annotations @ class level in exact order:

@runwith(powermockrunner.class) @preparefortest({ array1[].class, array2[].class }) 

inside class doing this:

array1[] test1= powermockito.mock(array1[].class); array2[] test2= powermockito.mock(array2[].class); 

and inside test method:

mockito.when(staticclass.somemethod()).thenreturn(test1); mockito.when(staticclass.somediffmethod()).thenreturn(test2); 

basically need mock array of interfaces. appreciated.

opening perspective on problem: think getting unit tests wrong.

you use mocking frameworks in order control behavior of individual objects provide code under test. there no sense in mocking array of something.

when "class under test" needs deal array, list, map, whatever, provide array, list, or map - make sure elements within array/collection ... need them. maybe array empty 1 test, maybe contains null test, , maybe contains mocked object third test.

meaning - don't do:

someinterface[] test1 = powermock.mock() ... 

instead do:

someinterface[] test1 = new someinterface[] { powermock.mock(someinterface.class) }; 

and allow notes:

  • at least in code, looks called interface "array1" , "array2". highly misleading. give interfaces names behavior about. fact later create arrays containing objects of interface ... doesn't matter @ all!
  • unless have reasons - consider not using powermock. powermock relies on byte-code manipulation; , can cause lot of problems. in situations, people wrote untestable code; , turn powermock somehow test that. correct answer rework broken design, , use mocking framework comes without "power" in name. can watch videos giving lengthy explanations how write testable code!

Comments

Popular posts from this blog

gridview - Yii2 DataPorivider $totalSum for a column -

java - Suppress Jboss version details from HTTP error response -

Sass watch command compiles .scss files before full sftp upload -