Skip to content

Commit

Permalink
Added MockDataSource on integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saulmm committed Jan 4, 2016
1 parent 9667b3f commit ab8352e
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ android {
buildConfigField "String", "MARVEL_PUBLIC_KEY", "\"${marvel_public_key}\""
buildConfigField "String", "MARVEL_PRIVATE_KEY", "\"${marvel_private_key}\""

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "saulmm.avengers.runner.AvengerTestRunner"
}

//noinspection GroovyAssignabilityCheck
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package saulmm.avengers.runner;

import android.app.Application;
import android.content.Context;
import android.support.test.internal.util.AndroidRunnerParams;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.runner.AndroidJUnitRunner;

import org.junit.runners.model.InitializationError;

import saulmm.avengers.tests.TestAvengersApplication;

/**
* Created by saulmm on 04/01/16.
*/
public class AvengerTestRunner extends AndroidJUnitRunner {

@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
String newApplicationClassName = TestAvengersApplication.class.getCanonicalName();
return super.newApplication(cl, newApplicationClassName, context);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package saulmm.avengers.tests;


import android.support.test.espresso.Espresso;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.espresso.intent.Intents;
Expand All @@ -15,10 +14,13 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

import saulmm.avengers.R;
import saulmm.avengers.injector.components.DaggerAvengersComponent;
import saulmm.avengers.injector.modules.ActivityModule;
import saulmm.avengers.repository.CharacterRepository;
import saulmm.avengers.rest.RestDataSource;
import saulmm.avengers.views.activities.CharacterDetailActivity;
import saulmm.avengers.views.activities.CharacterListActivity;

Expand All @@ -29,34 +31,19 @@
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
@LargeTest
@RunWith(AndroidJUnit4.class)
public class CharacterListActivityInstrumentationTest {

@Rule
public IntentsTestRule<CharacterListActivity> mCharacterListIntentRule =
new IntentsTestRule<>(CharacterListActivity.class);

@Before
public void setup() {
}

@Test
public void testThatAClickOnTheAvengerOPensTheDetailActivity() {
waitForRequest();

onView(ViewMatchers.withId(R.id.activity_avengers_recycler))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

intended(hasComponent(CharacterDetailActivity.class.getCanonicalName()));
}

public void waitForRequest() {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package saulmm.avengers.tests;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import rx.Observable;
import saulmm.avengers.entities.CollectionItem;
import saulmm.avengers.entities.MarvelCharacter;
import saulmm.avengers.repository.CharacterRepository;
import saulmm.avengers.rest.utils.deserializers.MarvelResultsDeserializer;

/**
* Created by saulmm on 04/01/16.
*/
public class MockRestDataSource implements CharacterRepository {

private final static Gson customGsonInstance;

static {
customGsonInstance = new GsonBuilder()
.registerTypeAdapter(new TypeToken<List<MarvelCharacter>>() {
}.getType(),
new MarvelResultsDeserializer<MarvelCharacter>())

.create();
}

@Override
public Observable<MarvelCharacter> getCharacter(int characterId) {
List<MarvelCharacter> marvelCharactersList = customGsonInstance.fromJson(
TestData.SINGLE_CHARACTER_JSON,
new TypeToken<List<MarvelCharacter>>() {}.getType());

return Observable.just(marvelCharactersList.get(0));
}

@Override
public Observable<List<MarvelCharacter>> getCharacters(int offset) {
List<MarvelCharacter> marvelCharactersList = customGsonInstance.fromJson(
TestData.TWENTY_CHARACTERS_JSON,
new TypeToken<List<MarvelCharacter>>() {}.getType());

return Observable.just(marvelCharactersList);
}

@Override
public Observable<List<CollectionItem>> getCharacterCollection(int characterId, String type) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package saulmm.avengers.tests;

import javax.inject.Singleton;

import dagger.Component;
import saulmm.avengers.AvengersApplication;
import saulmm.avengers.injector.AppModule;
import saulmm.avengers.injector.components.AppComponent;
import saulmm.avengers.repository.CharacterRepository;

@Singleton @Component(modules = TestAppModule.class)
public interface TestAppComponent extends AppComponent {
TestAvengersApplication app();
CharacterRepository dataRepository();
}
29 changes: 29 additions & 0 deletions app/src/androidTest/java/saulmm/avengers/tests/TestAppModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package saulmm.avengers.tests;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import saulmm.avengers.AvengersApplication;
import saulmm.avengers.repository.CharacterRepository;
import saulmm.avengers.rest.RestDataSource;

@Module
public class TestAppModule {

private final TestAvengersApplication mTestAvengersApplication;

public TestAppModule(TestAvengersApplication avengersApplication) {

this.mTestAvengersApplication = avengersApplication;
}

@Provides @Singleton TestAvengersApplication provideAvengersApplicationContext () { return mTestAvengersApplication; }

@Provides @Singleton CharacterRepository provideDataRepository () { return new MockRestDataSource(); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package saulmm.avengers.tests;

import saulmm.avengers.AvengersApplication;
import saulmm.avengers.injector.components.AppComponent;

public class TestAvengersApplication extends AvengersApplication {
private TestAppComponent mAppComponent;

@Override
public void onCreate() {
super.onCreate();
initializeInjector();
}

private void initializeInjector() {
mAppComponent = DaggerTestAppComponent.builder()
.testAppModule(new TestAppModule(this))
.build();
}

@Override
public TestAppComponent getAppComponent() {
return mAppComponent;
}
}
12 changes: 12 additions & 0 deletions app/src/androidTest/java/saulmm/avengers/tests/TestData.java

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions app/src/main/java/saulmm/avengers/injector/AppModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@

@Module
public class AppModule {

private final AvengersApplication mAvengersApplication;

public AppModule(AvengersApplication avengersApplication) {

this.mAvengersApplication = avengersApplication;
}

@Provides @Singleton AvengersApplication provideAvengersApplicationContext () { return mAvengersApplication; }
@Provides @Singleton
AvengersApplication provideAvengersApplicationContext() {
return mAvengersApplication; }

@Provides @Singleton CharacterRepository provideDataRepository (RestDataSource restDataSource) { return restDataSource; }
@Provides @Singleton
CharacterRepository provideDataRepository(RestDataSource restDataSource) {
return restDataSource; }
}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/adsf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/layout/asdf.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/layout/yep.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>

0 comments on commit ab8352e

Please sign in to comment.