Skip to content

Commit ee63a4f

Browse files
committed
Dagger, JUnit and Mockito example
1 parent 1f45f76 commit ee63a4f

File tree

35 files changed

+721
-0
lines changed

35 files changed

+721
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'com.neenbedankt.android-apt'
3+
4+
android {
5+
compileSdkVersion 23
6+
buildToolsVersion "23.0.3"
7+
defaultConfig {
8+
applicationId "com.vogella.android.daggerjunitmockito"
9+
minSdkVersion 23
10+
targetSdkVersion 23
11+
versionCode 1
12+
versionName "1.0"
13+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
24+
// Define versions in a single place
25+
26+
27+
28+
29+
ext {
30+
// App dependencies
31+
supportLibraryVersion = '23.4.0'
32+
guavaVersion = '18.0'
33+
glideVersion = '3.6.1'
34+
junitVersion = '4.12'
35+
mockitoVersion = '1.10.19'
36+
powerMockito = '1.6.2'
37+
hamcrestVersion = '1.3'
38+
runnerVersion = '0.5'
39+
rulesVersion = '0.5'
40+
espressoVersion = '2.2.2'
41+
DAGGER_VERSION ='2.4'
42+
}
43+
44+
dependencies {
45+
// Dependencies for local unit tests
46+
testCompile "junit:junit:$junitVersion"
47+
testCompile "org.mockito:mockito-all:$mockitoVersion"
48+
testCompile "org.hamcrest:hamcrest-all:$hamcrestVersion"
49+
testCompile "org.powermock:powermock-module-junit4:$powerMockito"
50+
testCompile "org.powermock:powermock-api-mockito:$powerMockito"
51+
testCompile 'com.google.dagger:dagger:2.4'
52+
compile 'com.google.dagger:dagger:2.4'
53+
apt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
54+
testApt "com.google.dagger:dagger-compiler:$DAGGER_VERSION"
55+
provided 'javax.annotation:jsr250-api:1.0'
56+
57+
compile fileTree(dir: 'libs', include: ['*.jar'])
58+
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
59+
compile 'javax.inject:javax.inject:1'
60+
61+
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
62+
androidTestCompile 'com.android.support.test:runner:0.5'
63+
androidTestCompile 'com.android.support:support-annotations:23.4.0'
64+
androidTestCompile 'org.mockito:mockito-core:2.0.57-beta'
65+
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
66+
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
67+
testCompile 'junit:junit:4.12'
68+
testCompile 'org.mockito:mockito-core:2.0.57-beta'
69+
testCompile 'junit:junit:4.12'
70+
testCompile 'javax.inject:javax.inject:1'
71+
72+
73+
74+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/android-sdks/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.filters.MediumTest;
6+
import android.support.test.runner.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
12+
import static org.junit.Assert.*;
13+
14+
/**
15+
* Instrumentation test, which will execute on an Android device.
16+
*
17+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
18+
*/
19+
@MediumTest
20+
@RunWith(AndroidJUnit4.class)
21+
public class ExampleInstrumentationTest {
22+
@Test
23+
public void useAppContext() throws Exception {
24+
// Context of the app under test.
25+
Context appContext = InstrumentationRegistry.getTargetContext();
26+
27+
assertEquals("com.vogella.android.daggerjunitmockito", appContext.getPackageName());
28+
}
29+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.daggerjunitmockito">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:supportsRtl="true"
10+
android:theme="@style/AppTheme">
11+
<activity android:name=".MainActivity">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN" />
14+
15+
<category android:name="android.intent.category.LAUNCHER" />
16+
</intent-filter>
17+
</activity>
18+
</application>
19+
20+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
6+
public class MainActivity extends Activity {
7+
8+
@Override
9+
protected void onCreate(Bundle savedInstanceState) {
10+
super.onCreate(savedInstanceState);
11+
setContentView(R.layout.activity_main);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import javax.inject.Inject;
4+
5+
/**
6+
* Created by vogella on 14.06.16.
7+
*/
8+
9+
public class MainService {
10+
private RestService restService;
11+
private MyPrinter printer;
12+
13+
@Inject
14+
public MainService(RestService restService,
15+
MyPrinter printer) {
16+
this.restService = restService;
17+
this.printer = printer;
18+
}
19+
20+
public void doSomething() {
21+
String s = restService.getSomething();
22+
printer.print(s.toUpperCase());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import javax.inject.Singleton;
4+
5+
import dagger.Component;
6+
7+
@Singleton
8+
@Component(modules = MyModule.class)
9+
public interface MyComponent {
10+
MainService mainService();
11+
12+
void inject(MainActivity mainActivity);
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import javax.inject.Singleton;
4+
5+
import dagger.Module;
6+
import dagger.Provides;
7+
8+
@Module
9+
public class MyModule {
10+
@Provides @Singleton
11+
public RestService provideRestService() {
12+
return new RestService();
13+
}
14+
15+
@Provides
16+
@Singleton public MyPrinter provideMyPrinter() {
17+
return new MyPrinter();
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
/**
4+
* Created by vogella on 14.06.16.
5+
*/
6+
7+
public class MyPrinter {
8+
9+
public void print(String s) {
10+
System.out.println(s);
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
/**
4+
* Created by vogella on 14.06.16.
5+
*/
6+
7+
public class RestService {
8+
public String getSomething() {
9+
return "Hello world";
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/activity_main"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context="com.vogella.android.daggerjunitmockito.MainActivity">
9+
10+
<TextView
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:text="Hello World!"
14+
app:layout_constraintBottom_toBottomOf="@+id/activity_main"
15+
app:layout_constraintLeft_toLeftOf="@+id/activity_main"
16+
app:layout_constraintRight_toRightOf="@+id/activity_main"
17+
app:layout_constraintTop_toTopOf="@+id/activity_main" />
18+
19+
</android.support.constraint.ConstraintLayout>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#3F51B5</color>
4+
<color name="colorPrimaryDark">#303F9F</color>
5+
<color name="colorAccent">#FF4081</color>
6+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">Dagger Junit Mockito</string>
3+
</resources>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<resources>
2+
3+
<!-- Base application theme. -->
4+
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
5+
<!-- Customize your theme here. -->
6+
</style>
7+
8+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() throws Exception {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.vogella.android.daggerjunitmockito;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
import org.junit.Test;
6+
7+
import javax.inject.Inject;
8+
9+
import dagger.Component;
10+
11+
import static org.mockito.Mockito.*;
12+
13+
public class MainServiceDaggerTest {
14+
15+
@Inject
16+
RestService restService;
17+
18+
@Inject MyPrinter myPrinter;
19+
20+
@Inject MainService mainService;
21+
22+
@Before
23+
public void setUp() {
24+
TestComponent build = DaggerTestComponent.builder().testModule(new TestModule()).build();
25+
build.inject(this);
26+
}
27+
28+
@Test
29+
public void testDoSomething() {
30+
when(restService.getSomething()).thenReturn("abc");
31+
32+
mainService.doSomething();
33+
34+
verify(myPrinter).print("ABC");
35+
}
36+
37+
// private Component verify(MyPrinter myPrinter) {
38+
// }
39+
}

0 commit comments

Comments
 (0)