I have been looking at this for an hour and can't seem to see what's wrong.
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("JSON", "hi");
String myString = jsonResponse.getString("JSON");
assertEquals("hi", myString);
Result... (The entire method throws Throwable). I tried try/catch as well. Same result....
java.lang.AssertionError:
Expected :hi
Actual :null
This is the class I am using
public class ECUserTests(){
@org.junit.Test
public void testUserExists(){
try{
JSONObject jsonResponse = new JSONObject();
jsonResponse.put("JSON", "Hello, World!");
String myString = jsonResponse.getString("JSON");
assertEquals("hi", myString);
}catch(JSONException e){
e.printStackTrace();
}
JSON has a special value called null which can be set on any type of data including arrays, objects, number and boolean types.
To check null in JavaScript, use triple equals operator(===) or Object is() method. If you want to use Object.is() method then you two arguments. 1) Pass your variable value with a null value.
Try with json. isNull( "field-name" ) . I would go further and say to NEVER use has(KEY_NAME), replacing those calls to ! isNull(KEY_NAME).
return Object.keys(obj).length === 0 ; This is typically the easiest way to determine if an object is empty.
I suppose you're running the unit test outside of Android and compiling against android.jar
. This lib contains only class and interface signatures without any implementation.
Solution:
Either execute the unit test in Android context (not on PC) or when running on PC, you have to load the "real" JSON library into your test.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With