I am struggling to write unit tests for my API implementation in Android. I would like to test the Retrofit functionality but run into concurrency problems, where I do not know how to ensure that the async API calls get executed and finish before I start testing the Android internal database calls.
Here is my test function:
public class postPrintModeTest extends ActivityInstrumentationTestCase2<MainActivity> implements IConstants {
public MainActivity activity;
public postPrintModeTest() {
super(MainActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
activity = getActivity();
String printModeName = "LSD Mode";
int parentId = 4;
Map<String, Object> payload = new HashMap<String, Object>();
payload.put("name", printModeName);
payload.put("parentId", parentId);
APIExec.getInstance().postPrintMode(IConstants.authorization, IConstants.userId, IConstants.deviceUid, payload); // <- this needs to finish before I execute the tests, so I have proper data in the database.
}
#SmallTest
public void testPrintModeCreated() {
DBPrintMode printMode = APIDBOps.getInstance().readPrintModeByPrintModeID(6);
assertNotNull("Print Mode does not exist", printMode);
}
#SmallTest
public void testPrintModeName() {
DBPrintMode printMode = APIDBOps.getInstance().readPrintModeByPrintModeID(6);
if(printMode != null)
{
assertTrue("Print Mode name is not correct", printMode.getName().equals("LSD Mode"));
}
}
}
and here is the async method in question:
public void postPrintMode(String authorization, final int userid, String deviceuid, final Map payload){
api.postPrintMode(authorization, userid, deviceuid, payload, new Callback<PrintMode>() {
#Override
public void success(PrintMode printMode, Response response) {
if (printMode.get_id() != 0) {
dbOps.writePrintMode(userid, printMode);
bus.getBus().post(new EVTNewPrintMode(printMode));
}
}
#Override
public void failure(RetrofitError retrofitError) {
retrofitError.printStackTrace();
APIUtils.showAPIResponseBody(retrofitError);
}
});
}
Related
I'm using RxJava3 and Live data.
I'm calling methos getAllMovies and getAllGenres in background thread, then set the received data to MutableLiveData.
In my splash activity I'm calling these methods from viewModel.
I have lottie animation , and each time lottie animation end, I'm checking if data received , if yes , opening another activity, else, waiting again for data receive.
The problem is when I lose internet connection , data is not received after the Internet is recovering.
I'm trying to run app when the wifi and internet is off, and then turning on wifi.
But always getting 0 in methods size.
If I running the app with internet on , data received normally.
That what I have.
App repository:
private MutableLiveData<List<GenreResult>> mGenresResponseMutableLiveData = new MutableLiveData<>();
public MutableLiveData<List<GenreResult>> getGenresResponseMutableLiveData() {
AppService appService = RetrofitInstance.getService();
appService.getAllGenres(mApplication.getResources().getString(R.string.api_key),
mApplication.getResources().getString(R.string.language))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<GenreResponse>() {
#Override
public void accept(GenreResponse genreResponses) throws Throwable {
mGenresResponseMutableLiveData.setValue(genreResponses.getGenreResults());
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Throwable {
Toast.makeText(mApplication.getApplicationContext(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
return mGenresResponseMutableLiveData;
}
ViewModel:
public class GenresViewModel extends AndroidViewModel {
private AppRepository mRepository;
public GenresViewModel(#NonNull Application application) {
super(application);
mRepository = new AppRepository(application);
}
public MutableLiveData<List<GenreResult>> getGenreLiveData() {
return mRepository.getGenresResponseMutableLiveData();
}
}
SplashActivity calling getGenre method:
private void getGenreList() {
mGenreResultArrayList = new ArrayList<>();
mGenresViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(GenresViewModel.class);
mGenresViewModel.getGenreLiveData().observe(this, new Observer<List<GenreResult>>() {
#Override
public void onChanged(List<GenreResult> genreResults) {
mGenreResultArrayList = (ArrayList<GenreResult>) genreResults;
}
});
}
SplashActivity checking on lottie listener if data received:
private void initViews() {
mLAVLoader = findViewById(R.id.lavLoader);
mLAVLoader.addAnimatorListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
if (mGenreResultArrayList != null && mGenreResultArrayList.size() > 0
&& mMovieResultArrayList != null && mMovieResultArrayList.size() > 0) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putParcelableArrayListExtra("genreList", mGenreResultArrayList);
intent.putParcelableArrayListExtra("moviesList", mMovieResultArrayList);
startActivity(intent);
} else {
mLAVLoader.playAnimation();
Log.d("myDebug", "onAnimationEnd: " + mGenreResultArrayList.size()+ " "+mMovieResultArrayList.size());
}
}
});
}
I fixed it with retryWhen method in Repository:
public MutableLiveData<List<MovieResult>> getAllMoviesMutableLiveData() {
AppService appService = RetrofitInstance.getService();
appService.getAllMovies(mApplication.getResources().getString(R.string.api_key),
mApplication.getResources().getString(R.string.language),
"popularity.desc")
.retryWhen(throwable ->
throwable.delay(5, TimeUnit.SECONDS))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<MoviesResponse>() {
#Override
public void accept(MoviesResponse moviesResponse) throws Throwable {
mAllMoviesMutableLiveData.setValue(moviesResponse.getMovieResults());
}
}, new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Throwable {
}
});
return mAllMoviesMutableLiveData;
}
I'm trying to build an app to fetch list of feed from server and display in Recyclerview. I am trying out basic implementation of LiveData like this.
I have set up an observer in my Fragment as follows:
viewModel = ViewModelProviders.of(getActivity()).get(SellViewModel.class);
viewModel.getSellItemList(19).observe(this, new Observer<List<LambdaSellRequestClass>>() {
#Override
public void onChanged(#Nullable List<LambdaSellRequestClass> sellItems) {
adapter.setSellEntities(sellItems);
}
});
My SellViewModel clas like this:
public class SellViewModel extends AndroidViewModel {
private SellRepository repository;
private MutableLiveData<List<LambdaSellRequestClass>> sellItems;
public SellViewModel(#NonNull Application application) {
super(application);
repository = new SellRepository(application);
try {
if (sellItems == null) {
sellItems = new MutableLiveData<>();
sellItems.postValue(repository.getSellItemList(user_id));
}
}catch (Exception e) {
Log.d("SELLFRAGMENT", "Error: " + e.getLocalizedMessage());
}
}
public MutableLiveData<List<LambdaSellRequestClass>> getSellItemList(int userId) throws ExecutionException, InterruptedException {
return sellItems;
}
}
My SellRepository like this:
public class SellRepository {
public SellRepository(Application application) {
}
public List<LambdaSellRequestClass> getSellItemList(int userId) throws ExecutionException, InterruptedException {
return new SellRepository.GetSellItemListAsync(SellRepository.this).execute(userId).get();
}
private static class GetSellItemListAsync extends AsyncTask<Integer, Void, List<LambdaSellRequestClass>> {
List<LambdaSellRequestClass> list = new ArrayList<>();
public GetSellItemListAsync() {
}
#Override
protected List<LambdaSellRequestClass> doInBackground(Integer... integers) {
final int userID = integers[0];
list =
lambdaFunctionsCalls.getSellItemByUser_lambda(requestClass).getSellItems();
return list;
}
}
My problem is when I add new sell items to database its not update mobile app.
I'm working on an Android app by adding a new functionality that fetch and save data with API calls.
These calls are made in a Fragment. There is a call made in an AsyncTask, and I don't want to create an AsyncTask for every call, so I just try send parameters to my controlles in some function, but when I debug every time I try to make a call without using an AsyncTask, I got an IOException "Cancelled". Is there a way to do this without using AsyncTasks in the same Fragment?
This is the AsyncTask:
private void validateUnit(#NonNull String unitCode, final int routeId, final boolean goodCondition) {
mUnitDetails = new UnitDetails();
if (mFindUnitAysncTask != null) {
mFindUnitAysncTask.cancel(true);
}
mFindUnitAysncTask = new AsyncTask<String, Void, FindUnitResponse>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
showProgressDialog();
}
#Override
protected FindUnitResponse doInBackground(String... params) {
FindUnitResponse unitResponse = mUnitController.findUnit(params[0], routeId);
FindUnitDetailsResponse unitDetailsResponse = mUnitController.getUnitDetails(
unitResponse.getUnits().get(0), mUser);
if(unitDetailsResponse.isSuccess()) {
mUnitDetails.setBranchCode(unitDetailsResponse.getBranchCode());
mUnitDetails.setBranchName(unitDetailsResponse.getBranchName());
mUnitDetails.setCompanyId(unitDetailsResponse.getCompanyId());
mUnitDetails.setEconomicNumber(unitDetailsResponse.getEconomicNumber());
mUnitDetails.setFuelType(unitDetailsResponse.getFuelType());
mUnitDetails.setFuelTypeId(unitDetailsResponse.getFuelTypeId());
mUnitDetails.setFuelPrice(unitDetailsResponse.getFuelPrice());
mUnitDetails.setModel(unitDetailsResponse.getModel());
mUnitDetails.setBrand(unitDetailsResponse.getBrand());
mUnitDetails.setUnitType(unitDetailsResponse.getUnitType());
mUnitDetails.setRouteCode(unitDetailsResponse.getRouteCode());
mUnitDetails.setRealTrips(unitDetailsResponse.getRealTrips());
mUnitDetails.setMaximumMileageRange(unitDetailsResponse.getMaximumMileageRange());
}
else {
showMessage(unitDetailsResponse.getMessage());
}
return unitResponse;
}
#Override
protected void onPostExecute(FindUnitResponse response) {
super.onPostExecute(response);
dismissProgressDialog();
if (response != null && response.isSuccess()) {
//Unit unit = response.getUnits().get(0);
unit = response.getUnits().get(0);
finishChecklist(unit, goodCondition);
} else {
showMessage(response.getMessage());
saveChecklist();
}
}
#Override
protected void onCancelled() {
super.onCancelled();
dismissProgressDialog();
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, unitCode);
}
With that I fetch the details of a vehicle. Then I have a method called validateMileage.
private void validateMileage(#NonNull Unit unit, #NonNull User user, #NonNull int mileage, int travels,
final boolean dayFinished) {
List<Incident> incidents = mIncidentController.getIncidentList();
Incident suspiciousMileageIncident = mIncidents.get(2);
List<Manager> managers = mManagersController.findByIncidentId(suspiciousMileageIncident.getId());
.....
}
If I just try to make calls like .getIncidentsList or .findByIncidentId I got an IOException when I wait for the response. But if I make the call in an AsyncTask, there is not errors.
I have a singleton to handle the registration and elimination of an entity Profilo ( a Profile).
This entity is set by passing an identifier and gathering information on the server in an async way.
My problem is that when I have to return my instance of profilo if it's not still loaded it will return null.
public class AccountHandler {
private static AccountHandler istanza = null;
Context context;
private boolean logged;
private Profilo profilo;
private AccountHandler(Context context) {
this.context = context;
//initialization
//setting logged properly
assignField(this.getName());
}
}
public static AccountHandler getAccountHandler(Context context) {
if (istanza == null) {
synchronized (AccountHandler.class) {
if (istanza == null) {
istanza = new AccountHandler(context);
}
}
}
return istanza;
}
public void setAccount(String nickname, String accessingCode) {
logged = true;
assignField(nickname);
}
//other methods
private void assignField(String nickname) {
ProfiloClient profiloClient = new ProfiloClient();
profiloClient.addParam(Profilo.FIELDS[0], nickname);
profiloClient.get(new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode,
Header[] headers,
JSONArray response) {
JSONObject objson = null;
try {
objson = (JSONObject) response.getJSONObject(0);
} catch (JSONException e) {
e.printStackTrace();
}
AccountHandler accountHandler = AccountHandler.getAccountHandler(context);
// Profilo is created with a JSONObject
// **setProfilo is called in async**
**accountHandler.setProfilo(new Profilo(objson));**
}
});
}
private void setProfilo(Profilo profilo) {
this.profilo = profilo;
}
public Profilo getProfilo() {
if( logged && profilo == null)
//How can I wait that profilo is loaded by the JsonHttpResponseHandler before to return it
return this.profilo;
}
}
Instead of calling getProfilo you could use a callback mechanism in which the AccountHandler class notifies the caller when the profile has been loaded. e.g.
public void setAccount(String nickname, String accessingCode, MyCallback cb) {
assignField(nickname, cb);
}
private void assignField(String nickname, MyCallback cb) {
....
accountHandler.setProfilo(new Profilo(objson));
cb.onSuccess(this.profilo);
}
Create an inner Interface MyCallback (rename it) in your AccountHandler class
public class AccountHandler {
public interface MyCallback {
void onSuccess(Profilo profile);
}
}
Now whenever you call setAccount you will pass the callback and get notified when the profile is available e.g.
accountHandler.setAccount("Test", "Test", new AccountHandler.MyCallback() {
void onSuccess(Profilio profile) {
// do something with the profile
}
}
I added, as #Murat K. suggested, an interface to my Class that will provide a method to be call with the object when it is ready to be used.
public class AccountHandler {
public interface Callback {
void profiloReady(Profilo profilo);
}
}
This method is called in getProfilo in a Handler that makes recursive calls to getProfilo until profilo is ready to be used, then it call the callback method which class is passed as argument of getProfilo.
public void getProfilo(final Callback Callback) {
if( logged && (profilo == null || !profilo.isReady() ) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
getProfilo(Callback);
}
}, 500);
}else
Callback.profiloReady(profilo);
}
Example of getProfilo call
public class ProfiloCall implements AccountHandler.MyCallback {
#Override
public void profiloReady(Profilo profilo) {
//Use profilo as needed
//EXECUTED ONLY WHEN PROFILO IS READY
}
public void callerMethod() {
//useful code
accountHandler.getProfilo(this);
//other useful code
}
}
In my Android application I have set up Volley.
Robolectric.application is initialized and all other tests runs smoothly.
I get this error when trying to get mocked HTTP response.
This is my test:
#RunWith(MyRobolectricTestRunner.class)
public class ApiTests {
#Inject
protected Api api;
#Before
public void setUp() {
ObjectGraph.create(new AndroidModule(Robolectric.application), new TestApplicationModule()).inject(this);
}
#Test
public void shouldGetErrorList() throws Exception {
Project project = new Project("test", "test", "test", DateTime.now());
addPendingProjectsErrorsResponse("response.json"); //adding response to FakeHttpLayer
api.getProjectErrors(project, new Listener<ProjectErrors>() {
#Override
public void onResponse(ProjectErrors response) {
assertNotNull(response);
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
throw new RuntimeException(error);
}
}
);
}
}
This is error I get:
Exception in thread "Thread-3" java.lang.NullPointerException
at org.robolectric.shadows.ShadowLooper.getMainLooper(ShadowLooper.java:59)
at android.os.Looper.getMainLooper(Looper.java)
at org.robolectric.Robolectric.getUiThreadScheduler(Robolectric.java:1301)
at org.robolectric.shadows.ShadowSystemClock.now(ShadowSystemClock.java:15)
at org.robolectric.shadows.ShadowSystemClock.uptimeMillis(ShadowSystemClock.java:25)
at org.robolectric.shadows.ShadowSystemClock.elapsedRealtime(ShadowSystemClock.java:30)
at android.os.SystemClock.elapsedRealtime(SystemClock.java)
at com.android.volley.VolleyLog$MarkerLog.add(VolleyLog.java:114)
at com.android.volley.Request.addMarker(Request.java:174)
at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:92)
I had same error and avoid it by using my own (and ugly) SystemClock shadow.
shadow class:
#Implements(value = SystemClock.class, callThroughByDefault = true)
public static class MyShadowSystemClock {
public static long elapsedRealtime() {
return 0;
}
}
test code:
#Test
#Config(shadows = { MyShadowSystemClock.class, ... })
public void myTest() {
}
Another workaround would be to disable Volley logging by calling
VolleyLog.DEBUG = false;
in your setUp method.