Android Open Source - libraryofalexandria Art Work Model Tests






From Project

Back to project page libraryofalexandria.

License

The source code is released under:

Apache License

If you think the Android project libraryofalexandria 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 alexandria.israelferrer.com.libraryofalexandria;
//from ww  w .  j a  va  2 s.  co  m
import android.content.SharedPreferences;

import junit.framework.TestCase;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import static alexandria.israelferrer.com.libraryofalexandria.ArtWorkListActivityTests.ARTWORKS;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.only;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public class ArtWorkModelTests extends TestCase {
    private ArtWorkModel model;
    private SharedPreferences preferences;
    private ArtWorkService service;
    private Callback callback;
    private Exception exception;

    @Override
    public void setUp() throws Exception {
        super.setUp();

        preferences = mock(SharedPreferences.class);
        service = mock(ArtWorkService.class);
        callback = mock(Callback.class);
        model = new ArtWorkModel(service, preferences);
        exception = new Exception();
    }

    public void testGetArtWorks_success() throws Exception {
        serviceSuccessAnswer();
        model.getArtWorks(callback);
        verify(service, only()).getArtWorks(any(Callback.class));
        verify(callback, only()).success(ARTWORKS);
        verify(preferences, times(ARTWORKS.size())).getFloat(any(String.class), eq(0F));
    }

    private void serviceSuccessAnswer() {
        doAnswer(new Answer() {
            public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                ((Callback) args[0]).success(ARTWORKS);
                return null;
            }
        }).when(service).getArtWorks(any(Callback.class));
    }

    public void testGetArtWorks_failure() throws Exception {
        serviceFailureAnswer();
        model.getArtWorks(callback);
        verify(service, only()).getArtWorks(any(Callback.class));
        verify(callback, only()).failure(exception);
    }

    private void serviceFailureAnswer() {
        doAnswer(new Answer() {
            public Object answer(InvocationOnMock invocation) {
                Object[] args = invocation.getArguments();
                ((Callback) args[0]).failure(exception);
                return null;
            }
        }).when(service).getArtWorks(any(Callback.class));
    }
}




Java Source Code List

alexandria.israelferrer.com.libraryofalexandria.ArtWorkListActivityTests.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkListActivity.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkListPresenter.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkModelTests.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkModel.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkPersistence.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkPresenterTests.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkServiceImpl.java
alexandria.israelferrer.com.libraryofalexandria.ArtWorkService.java
alexandria.israelferrer.com.libraryofalexandria.ArtWork.java
alexandria.israelferrer.com.libraryofalexandria.Callback.java
alexandria.israelferrer.com.libraryofalexandria.Display.java
alexandria.israelferrer.com.libraryofalexandria.ItemView.java
alexandria.israelferrer.com.libraryofalexandria.Model.java
alexandria.israelferrer.com.libraryofalexandria.Presenter.java