vendredi 20 mai 2016

Android Test. JSON Object

Im trying to do some test in Android but I have no idea how to do them. For example for this function. I need your help to test optDate function.

public static Date optDate(JSONObject json, String key) {
    if(!json.isNull(key)) {
        try {
            DateTime dt = JSONUtils.dateFormatter.withZoneUTC().parseDateTime(json.optString(key));
            Date date = dt.toDate();
            return date;
        } catch(Exception e) {
        }
    }
    return null;
}

I have something like this.

@RunWith(MockitoJUnitRunner.class)

public class JSONUtilsTest {

@Mock
JSONObject JSONMock = Mockito.mock(JSONObject.class);
static String dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
static DateTimeFormatter dateFormatter = DateTimeFormat.forPattern(dateFormat);
DateTime dt = JSONUtilsTest.dateFormatter.withZoneUTC().parseDateTime("2011-11-02T02:50:12.208Z");
Date date = dt.toDate();

@Before
public void setup() {
    when(JSONUtils.optString(JSONMock, "name")).thenReturn("Daniel");
    when(JSONUtils.optString(JSONMock, "city")).thenReturn(null);
    when(JSONUtils.optString(JSONMock, "lastname")).thenReturn("");
    when(JSONMock.optString("possessionDate")).thenReturn("");
}

@Test
public void optDateShouldReturnValueOnValidString() {
        Date result = JSONUtils.optDate(JSONMock, "possessionDate");
        Assert.assertEquals("a",result);

// Assert.assertNull(result); }

I would apretiate your help.

Aucun commentaire:

Enregistrer un commentaire