Saturday, 15 August 2015

java - How to get a JsonProcessingException using Jackson -



java - How to get a JsonProcessingException using Jackson -

might unusual question indeed accomplish a bit more coverage on tests , although coded against jsonprocessingexception can't create payload generates exception, maybe because jackson quite smart , converts string, , bad strings goes around json specs. problem jackson quite :)

i want payload when run this, break jsonprocessingexception:

string jsonpayload = objectmapper.writevalueasstring(payload);

i've tried like:

hashmap<string, string> invalidjsonpayload= new hashmap<>(); invalidjsonpayload.put("021",021); invalidjsonpayload.put("---",021); invalidjsonpayload.put("~",021);

i'm not fussed type, sense free suggest one. empty object example, throws jsonmappingexception , grab 1 well.

i wanted same thing, , accomplished using mockito "spy" function, wraps real object mock object. calls mock object forwarded real object, except trying mock. example:

objectmapper om = mockito.spy( new objectmapper ); mockito.when( om.writevalueasstring( errorobject.class )).thenthrow( new jsonprocessingexception("") {});

all usages of om handled underlying objectmapper instance until instance of errorobject gets passed in, @ point jsonprocessingexception thrown.

the newjsonprocessingexception created anonymous class, protected class , sub-class can instantiated.

java json jackson

No comments:

Post a Comment