Java tutorial
package cl.smartcities.isci.transportinspector.backend; import android.os.Parcel; import android.support.test.runner.AndroidJUnit4; import android.test.suitebuilder.annotation.SmallTest; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @RunWith(AndroidJUnit4.class) @SmallTest public class BusTest { private static final String SERVICE = "506"; private static final String LICENSE_PLATE = "GGWP00"; private static final String ERROR = "some error"; private static final String TIME = "Menos de 5 min."; private static final String DISTANCE = "Menos de 10 mts."; private static final String DISTANCEV2 = "10"; private static final String DIRECTION = "left"; private static final int PASSENGERS = 2; private static final double LAT = -90.3; private static final double LONG = 50.45; private static final int COLOR = 2; private static final boolean RANDOM = true; private Bus bus; @Before public void createBus() { bus = new Bus(SERVICE, LICENSE_PLATE); } @Test public void busParcelWriteReadTest() { Parcel parcel = Parcel.obtain(); bus.writeToParcel(parcel, bus.describeContents()); parcel.setDataPosition(0); Bus createdFromParcel = Bus.CREATOR.createFromParcel(parcel); assertThat(createdFromParcel.getService(), is(SERVICE)); assertThat(createdFromParcel.getLicensePlate(), is(LICENSE_PLATE)); assertThat(createdFromParcel.getActiveMapBusIcon(), is(bus.getActiveMapBusIcon())); assertThat(createdFromParcel.getBusIcon(), is(bus.getBusIcon())); assertThat(createdFromParcel.getDistance(), is(bus.getDistance())); assertThat(createdFromParcel.getTime(), is(bus.getTime())); } @Test public void invalidBusJsonTest() throws JSONException { JSONObject json = new JSONObject(); try { json.put("valido", 0); json.put("servicio", SERVICE); json.put("descripcionError", ERROR); } catch (JSONException e) { e.printStackTrace(); } Bus bus = new Bus(json); assertThat(bus.getService(), is(SERVICE)); assertThat(bus.getValid(), is(0)); assertThat(bus.getDescriptionError(), is(ERROR)); } @Test public void validBusJsonTest() throws JSONException { JSONObject json = new JSONObject(); try { json.put("valido", 1); json.put("servicio", SERVICE); json.put("patente", LICENSE_PLATE); json.put("tiempoV2", TIME); json.put("distanciaV2", DISTANCE); json.put("sentido", DIRECTION); json.put("tienePasajeros", PASSENGERS); json.put("color", COLOR); json.put("random", false); json.put("lat", LAT); json.put("lon", LONG); json.put("distanciaMts", DISTANCEV2); json.put("eventos", new JSONArray()); } catch (JSONException e) { e.printStackTrace(); } Bus bus = new Bus(json); assertThat(bus.getLatitude(), is(LAT)); assertThat(bus.getLongitude(), is(LONG)); } @Test public void validBusJsonTest2() throws JSONException { JSONObject json = new JSONObject(); try { json.put("valido", 1); json.put("servicio", SERVICE); json.put("patente", LICENSE_PLATE); json.put("tiempoV2", TIME); json.put("distanciaV2", DISTANCE); json.put("sentido", DIRECTION); json.put("tienePasajeros", PASSENGERS); json.put("color", COLOR); json.put("random", RANDOM); json.put("distanciaMts", DISTANCEV2); json.put("eventos", new JSONArray()); } catch (JSONException e) { e.printStackTrace(); } Bus bus = new Bus(json); assertThat(bus.getService(), is(SERVICE)); assertThat(bus.getLicensePlate(), is(LICENSE_PLATE)); assertThat(bus.getTime(), is(TIME)); assertThat(bus.getFormattedDistance(), is(DISTANCE)); assertThat(bus.getDirection(), is(DIRECTION)); assertThat(bus.getAmountOfReports(), is(0)); assertThat(bus.getPassengerNumber(), is(PASSENGERS)); assertThat(bus.getLatitude(), is(Double.POSITIVE_INFINITY)); assertThat(bus.getLongitude(), is(Double.POSITIVE_INFINITY)); } }