Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEMO dagger2 androidTest #25

Open
eurosecom opened this issue Aug 25, 2017 · 5 comments
Open

DEMO dagger2 androidTest #25

eurosecom opened this issue Aug 25, 2017 · 5 comments

Comments

@eurosecom
Copy link
Owner

eurosecom commented Aug 25, 2017

By http://blog.sqisland.com/2015/04/dagger-2-espresso-2-mockito.html
Github code https://github.com/chiuki/android-test-demo
Override dagger Module by androidTesting. Demo App in folder android-test-demo-master

  • override TestRunner in app/build.gradle

defaultConfig {
applicationId 'com.sqisland.android.test_demo'
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName '1.0.0'

testInstrumentationRunner 'com.sqisland.android.test_demo.MockTestRunner'

}
In MockTestRunner we call MockDemoApplication.java

  • app/buld.gradle dependency androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.11" for build components and modules in TestActivity for DaggerMainActivityTest_TestComponent.builder().build(); in MockDemoApplication.java

  • edit Configuration MainActivityTest.java to change TestRunner to MockTestRunner

eurosecom added a commit that referenced this issue Aug 25, 2017
#25
- demo androidTesting app
@eurosecom
Copy link
Owner Author

eurosecom commented Aug 25, 2017

  • best approach to create dagger2 component is create classic components without dependent component and subcomponent . It's easy to override classic component in MockApplication.java and ActivityTest.java without change app code only in testcode.

  • is better provide dependencies in Module.java instead of base injection in class constructors because of easy override provided dependency in androidTestActivity

  • ApplicationModule.java included to ClockModule.java

`@Module(includes = {ApplicationModule.class} )
public class ClockModule {

@provides
@singleton
Clock provideClock() {
return new Clock();
}

@provides
@singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}

}`

eurosecom added a commit that referenced this issue Aug 25, 2017
#25
- change create DaggerMainActivityTest_TestComponent in
MockDemoApplication.java after to add ApplicationModule to
DemoComponent.java for provide SharedPreferences
eurosecom added a commit that referenced this issue Aug 25, 2017
#25
- edit creating DemoComponent in DemoApplication.java
eurosecom added a commit that referenced this issue Aug 26, 2017
#25
- add DgAllEmpsAbsMvvmActivity
- add EmptyFragment
@eurosecom
Copy link
Owner Author

ADD Lambda Expression Support

  • add to project/build.gradle
    dependencies { classpath 'com.android.tools.build:gradle:2.3.3' classpath 'me.tatarka:gradle-retrolambda:3.5.0' }

  • add to app/build.gradle

`apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {
compileSdkVersion 23
buildToolsVersion '25.0.2'

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
`

@eurosecom
Copy link
Owner Author

Add Realm

  • add to app/build.gradle

`dependencies {

// Realm
compile 'io.realm:realm-android:0.82.1'`

@eurosecom
Copy link
Owner Author

Add RxJava and RxAndroid

  • add to app/build.gradle

`dependencies {

//rxjava
compile 'io.reactivex.rxjava2:rxjava:2.0.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex:rxandroid:1.2.0'
`

  • because of Duplicate files copied in APK META-INF/rxjava.properties add to app/build.gradle

packagingOptions { //duplicate files copied in apk meta-inf/rxjava exclude 'META-INF/rxjava.properties' }

eurosecom added a commit that referenced this issue Aug 27, 2017
#25
- add Realm, RxJava and RxAndroid
eurosecom added a commit that referenced this issue Aug 29, 2017
#25
- add DgAllEmpsAbsListFragment.java
- inject DemoComponent and provide ClockModule.java
eurosecom added a commit that referenced this issue Aug 30, 2017
#25
- create DgActivityTest.java for DgAllEmpsAbsMvvmActivity.java
- new DgMockTestRunner.java, DgMockDemoApplication
- in app/build.gradle call new  DgMockTestRunner.java

TODO
- i can not call two TestRunner in build.gradle
- change call component in one DemoApplication.java
eurosecom added a commit that referenced this issue Aug 30, 2017
#25
- i can not call two TestRunner in app/build.gradle
I have to change separated createcomponent and createdgcomponent in one
DemoApplication.java and one TestRunner
eurosecom added a commit that referenced this issue Aug 31, 2017
#25
- overriding modules work ok in activity and fragment
- attention - what component you inject in activity that component you
have to override in tastactivity
@eurosecom
Copy link
Owner Author

  • i have created one MockTestRunner.java that call one DemoApplication.java
  • in DemoApplication.java are separated createcomponent method for each component
  • the testactivity override createcomponent method in DemoApplication.java with appropriated module
  • overriding modules work ok in activity and fragment
  • attention, what component you inject in activity that component you have to override in tastactivity

eurosecom added a commit that referenced this issue Sep 1, 2017
#25
- override new component in DgAeaActivityTest.java work good

TODO
- override bind in androidTest because of i want to test and mock only
getObservableFBusersRealmEmployee()
eurosecom added a commit that referenced this issue Sep 11, 2017
#25
- i do not override bind in androidTest because of i want to  mock next
methods of mViewModel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant