java - Mocking Generic types using Mockito -


i writing test case using junit , mockito rest services using jersey. getting null object instead of mocked object response class.

code under test

response response = builder                         .put( entity.entity( new bytearrayinputstream( jsonobj.tostring().getbytes() ), mediatype.application_json ), response.class ); 

test case:

private invocation.builder builder;  private entity<bytearrayinputstream> inputstream; private response response; @before public void setup() throws exception {     builder = mock( invocation.builder.class );     inputstream = (entity<bytearrayinputstream>)mock( entity.class );     response = mock( response.class ); }  @test public void mytest() { when( builder.put( inputstream, response.class ) ).thenreturn( response ); } 

so line of code gives me null response. there other way this.

thanks.

that because mixing various things.

your production code does:

entity.entity( new bytearrayinputstream( ... 

so, got there is:

  • a static call (entity.entity())
  • a call new

both of these operations can not mocked mockito. simple that.

in order mock static method calls, have frameworks such powermock(ito) or jmockit.

but rather recommend different solution: consider reworking production code. instead making static+new call; create like

interface entityprovider {   public entity of(bytes[] data, mediatype type); } 

you can create impl class uses current code - testing purposes, can dependency-inject mock of interface (created via mockito.mock()); , of sudden, whole code becomes testable mockito again.

and no need other mocking frameworks (and imho; @ least powermockito comes amount of cost - not simple "just switch other framework").


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -