-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MockDataSource on integration tests
- Loading branch information
Showing
12 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
app/src/androidTest/java/saulmm/avengers/runner/AvengerTestRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
app/src/androidTest/java/saulmm/avengers/tests/MockRestDataSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
app/src/androidTest/java/saulmm/avengers/tests/TestAppComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
app/src/androidTest/java/saulmm/avengers/tests/TestAppModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); } | ||
} |
30 changes: 30 additions & 0 deletions
30
app/src/androidTest/java/saulmm/avengers/tests/TestAvengersApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
app/src/androidTest/java/saulmm/avengers/tests/TestData.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |