java - mocked JSONObject cannot retrieve value that was added -


i think there's wrong when i'm trying spy jsonobject using mockito. here's code unit test method:

jsonobject json = new jsonobject(); json.put("token","value");  jsonobject spyjson = mockito.spy(jsonobject.class); powermockito.whennew(jsonobject.class).withanyarguments().thenreturn(spyjson); service.gettoken(json.tostring()); 

here's beggining of method gettoken():

public loginresponsedata gettoken(string response) throws jsonexception {     jsonobject resjson = new jsonobject(response); //resjson = {} here      //do stuff      } 

in build.gradle i've added: testcompile 'org.json:json:20140107'

i think code have written fine. make sure library(java-json.jar) jar using correct one.

i've re written same code of yours in custom created class jsontest bellow , works fine.

i've downloaded java-jason.jar form here has same package mentioned in comment "org.json.jsonobject" (you can try using linked jar project).

below working code snippet:

import org.json.jsonexception; import org.json.jsonobject;      public class jsontest {         public static void main(string[] args) throws jsonexception {             jsonobject json = new jsonobject();             json.put("key", "value");             string jsonstring = json.tostring();              string key = new jsonobject(jsonstring).getstring("key");             system.out.println(key);          }     } 

output:

value 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -