Android Open Source - android-javarx-example Movie Service Test






From Project

Back to project page android-javarx-example.

License

The source code is released under:

Apache License

If you think the Android project android-javarx-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.bnsantos.movies;
//from   ww w  .  j  ava  2  s .c  om
import com.bnsantos.movies.model.Movie;
import com.bnsantos.movies.model.MovieListType;
import com.bnsantos.movies.model.MovieResponse;
import com.bnsantos.movies.services.MovieService;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.robolectric.annotation.Config;

import java.util.ArrayList;

import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by bruno on 19/11/14.
 */
@Config(emulateSdk = 18)
@RunWith(MoviesTestRunner.class)
public class MovieServiceTest {
    MovieResponse mMovieResponse;
    private MovieService mMovieService;

    @Before
    public void setUp() throws Exception {
        mMovieService = Mockito.mock(MovieService.class);
        mMovieResponse = new MovieResponse();
        mMovieResponse.setLink_template("LinkTemplate");
        mMovieResponse.setMovies(TestUtils.movieListGenerator(10));
    }

    @Test
    public void testFetchingMovies() throws Exception {
        Observable<MovieResponse> responseObservable = Observable.create(new Observable.OnSubscribe<MovieResponse>() {
            @Override
            public void call(Subscriber<? super MovieResponse> subscriber) {
                subscriber.onNext(mMovieResponse);
                subscriber.onCompleted();
            }
        });
        Mockito.when(mMovieService.retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), "token", 10, 1, "us")).thenReturn(responseObservable);
        MovieResponse response = mMovieService.retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), "token", 10, 1, "us").toBlocking().single();
        Assert.assertEquals(response, mMovieResponse);
    }

    @Test
    public void testFetchingEmptyMovies() throws Exception {
        mMovieResponse.setMovies(new ArrayList<Movie>());
        Observable<MovieResponse> responseObservable = Observable.create(new Observable.OnSubscribe<MovieResponse>() {
            @Override
            public void call(Subscriber<? super MovieResponse> subscriber) {
                subscriber.onNext(mMovieResponse);
                subscriber.onCompleted();
            }
        });
        Mockito.when(mMovieService.retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), "token", 10, 1, "us")).thenReturn(responseObservable);

        MovieResponse response = mMovieService.retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), "token", 10, 1, "us").toBlocking().single();
        Assert.assertEquals(response, mMovieResponse);
    }

    @Test
    public void testFetchingMoviesError() throws Exception {
        Observable<MovieResponse> responseObservable = Observable.create(new Observable.OnSubscribe<MovieResponse>() {
            @Override
            public void call(Subscriber<? super MovieResponse> subscriber) {
                subscriber.onError(new Exception("InvalidToken"));
            }
        });
        Mockito.when(mMovieService.retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), "", 10, 1, "us")).thenReturn(responseObservable);
        Observable<MovieResponse> movieResponseObservable = mMovieService.retrieveMovies(MovieListType.IN_THEATERS.name().toLowerCase(), "", 10, 1, "us");
        Assert.assertNotNull(movieResponseObservable);
        movieResponseObservable.subscribe(new Action1<MovieResponse>() {
            @Override
            public void call(MovieResponse movieResponse) {

            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Assert.assertNotNull(throwable);
                Assert.assertEquals(throwable.getMessage(), "InvalidToken");
            }
        }, new Action0() {
            @Override
            public void call() {
                Assert.assertEquals(true, true);
            }
        });
    }
}




Java Source Code List

com.bnsantos.movies.App.java
com.bnsantos.movies.MovieServiceTest.java
com.bnsantos.movies.MoviesTestRunner.java
com.bnsantos.movies.TestSuite.java
com.bnsantos.movies.TestUtils.java
com.bnsantos.movies.Utils.java
com.bnsantos.movies.activities.MainActivity.java
com.bnsantos.movies.adapter.MovieAdapter.java
com.bnsantos.movies.cache.MovieCaching.java
com.bnsantos.movies.cache.PersistentManager.java
com.bnsantos.movies.model.Links.java
com.bnsantos.movies.model.MovieListType.java
com.bnsantos.movies.model.MovieResponse.java
com.bnsantos.movies.model.Movie.java
com.bnsantos.movies.model.Posters.java
com.bnsantos.movies.model.Ratings.java
com.bnsantos.movies.model.ReleaseDates.java
com.bnsantos.movies.providers.LazyMovieProvider.java
com.bnsantos.movies.providers.MovieProvider.java
com.bnsantos.movies.providers.SubjectMovieProvider.java
com.bnsantos.movies.services.MovieService.java
com.bnsantos.movies.services.RestErrorHandler.java