Skip to content

Commit 3b9ee68

Browse files
committed
Adds JUnit examples
1 parent 291f921 commit 3b9ee68

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1405
-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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 24
5+
buildToolsVersion "24.0.0"
6+
defaultConfig {
7+
jackOptions {
8+
enabled true
9+
}
10+
applicationId "com.vogella.android.test.juntexamples"
11+
minSdkVersion 23
12+
targetSdkVersion 24
13+
versionCode 1
14+
versionName "1.0"
15+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
16+
}
17+
buildTypes {
18+
release {
19+
minifyEnabled false
20+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21+
}
22+
}
23+
24+
compileOptions {
25+
sourceCompatibility JavaVersion.VERSION_1_8
26+
targetCompatibility JavaVersion.VERSION_1_8
27+
}
28+
29+
}
30+
31+
32+
33+
dependencies {
34+
compile fileTree(dir: 'libs', include: ['*.jar'])
35+
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha3'
36+
testCompile 'junit:junit:4.12'
37+
testCompile "org.mockito:mockito-all:1.10.19"
38+
testCompile 'org.hamcrest:hamcrest-library:1.3'
39+
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
40+
androidTestCompile 'com.android.support.test:runner:0.5'
41+
androidTestCompile 'com.android.support:support-annotations:24.0.0'
42+
testCompile "org.mockito:mockito-all:1.10.19"
43+
androidTestCompile 'org.mockito:mockito-core:2.0.57-beta'
44+
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
45+
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
46+
}
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/Sdk/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.test.juntexamples;
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.test.juntexamples", appContext.getPackageName());
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.vogella.android.test.juntexamples;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.filters.MediumTest;
6+
import android.support.test.rule.ActivityTestRule;
7+
import android.support.test.runner.AndroidJUnit4;
8+
import android.view.View;
9+
import android.widget.ArrayAdapter;
10+
import android.widget.ListAdapter;
11+
import android.widget.ListView;
12+
import android.widget.TextView;
13+
14+
import org.junit.Rule;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
18+
import static org.hamcrest.Matchers.greaterThan;
19+
import static org.hamcrest.Matchers.hasSize;
20+
import static org.hamcrest.Matchers.instanceOf;
21+
import static org.hamcrest.Matchers.notNullValue;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertThat;
25+
import static org.junit.Assert.fail;
26+
27+
/**
28+
* Instrumentation test, which will execute on an Android device.
29+
*
30+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
31+
*/
32+
@MediumTest
33+
@RunWith(AndroidJUnit4.class)
34+
public class MainActivityTest {
35+
36+
@Rule
37+
public ActivityTestRule<MainActivity> rule = new ActivityTestRule<>(MainActivity.class);
38+
39+
@Test
40+
public void ensureListViewIsPresent() throws Exception {
41+
MainActivity activity = rule.getActivity();
42+
View viewById = activity.findViewById(R.id.listview);
43+
viewById.findViewById(R.id.listview);
44+
assertThat(viewById,notNullValue());
45+
assertThat(viewById, instanceOf(ListView.class));
46+
ListView listView = (ListView) viewById;
47+
ListAdapter adapter = listView.getAdapter();
48+
assertThat(adapter, instanceOf(ArrayAdapter.class));
49+
assertThat(adapter.getCount(), greaterThan(5));
50+
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.vogella.android.test.juntexamples;
2+
3+
import android.content.pm.PackageInfo;
4+
import android.test.ApplicationTestCase;
5+
import android.test.MoreAsserts;
6+
7+
import org.junit.Test;
8+
9+
/**
10+
* Created by vogella on 19.06.16.
11+
*/
12+
13+
public class MyApplicationTest extends ApplicationTestCase<MyApplication> {
14+
15+
private MyApplication application;
16+
17+
public MyApplicationTest() {
18+
super(MyApplication.class);
19+
}
20+
21+
protected void setUp() throws Exception {
22+
super.setUp();
23+
createApplication();
24+
application = getApplication();
25+
26+
}
27+
28+
29+
public void testCorrectVersion() throws Exception {
30+
PackageInfo info = application.getPackageManager().getPackageInfo(application.getPackageName(), 0);
31+
assertNotNull(info);
32+
MoreAsserts.assertMatchesRegex("\\d\\.\\d", info.versionName);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.vogella.android.test.juntexamples;
2+
3+
import android.content.Intent;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.filters.MediumTest;
6+
import android.support.test.rule.ActivityTestRule;
7+
import android.support.test.runner.AndroidJUnit4;
8+
import android.view.View;
9+
import android.widget.ListView;
10+
import android.widget.TextView;
11+
12+
import org.junit.Rule;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
16+
import static org.hamcrest.Matchers.instanceOf;
17+
import static org.hamcrest.Matchers.is;
18+
import static org.hamcrest.Matchers.notNullValue;
19+
import static org.junit.Assert.assertThat;
20+
21+
/**
22+
* Instrumentation test, which will execute on an Android device.
23+
*
24+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
25+
*/
26+
@MediumTest
27+
@RunWith(AndroidJUnit4.class)
28+
public class SecondActivityTest {
29+
30+
@Rule
31+
public ActivityTestRule<SecondActivity> rule = new ActivityTestRule<SecondActivity>(SecondActivity.class) ;
32+
// {
33+
// @Override
34+
// protected Intent getActivityIntent() {
35+
// InstrumentationRegistry.getTargetContext();
36+
// Intent intent = new Intent(Intent.ACTION_MAIN);
37+
// intent.putExtra("MYKEY", "Hello");
38+
// return intent;
39+
// }
40+
41+
42+
// };
43+
44+
@Test
45+
public void ensureIntentDataIsDisplayed() throws Exception {
46+
SecondActivity activity = rule.getActivity();
47+
48+
View viewById = activity.findViewById(R.id.target);
49+
50+
assertThat(viewById,notNullValue());
51+
assertThat(viewById, instanceOf(TextView.class));
52+
TextView textView = (TextView) viewById;
53+
// InstrumentationRegistry.getTargetContext().wait(10000);
54+
assertThat(textView.getEditableText().toString(),is("Hello"));
55+
}
56+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.test.juntexamples">
4+
5+
<application
6+
android:name="MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
<activity android:name=".SecondActivity"></activity>
20+
</application>
21+
22+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
2+
package com.vogella.android.test.juntexamples;
3+
4+
import android.util.ArrayMap;
5+
6+
import com.vogella.android.test.juntexamples.model.Movie;
7+
import com.vogella.android.test.juntexamples.model.Race;
8+
import com.vogella.android.test.juntexamples.model.Ring;
9+
import com.vogella.android.test.juntexamples.model.TolkienCharacter;
10+
11+
12+
import java.util.ArrayList;
13+
import java.util.Comparator;
14+
import java.util.Date;
15+
import java.util.HashMap;
16+
import java.util.List;
17+
import java.util.Map;
18+
19+
import static com.vogella.android.test.juntexamples.model.Race.DWARF;
20+
import static com.vogella.android.test.juntexamples.model.Race.ELF;
21+
import static com.vogella.android.test.juntexamples.model.Race.HOBBIT;
22+
import static com.vogella.android.test.juntexamples.model.Race.MAIA;
23+
import static com.vogella.android.test.juntexamples.model.Race.MAN;
24+
import static com.vogella.android.test.juntexamples.model.Race.ORC;
25+
26+
/**
27+
* Init data for assertions examples.
28+
*/
29+
public class DataService {
30+
31+
static final String ERROR_MESSAGE_EXAMPLE_FOR_ASSERTION = "{} assertion : {}\n";
32+
33+
34+
// Some of the Lord of the Rings characters :
35+
final TolkienCharacter frodo = new TolkienCharacter("Frodo", 33, HOBBIT);
36+
37+
final TolkienCharacter sam = new TolkienCharacter("Sam", 38, HOBBIT);
38+
39+
final TolkienCharacter merry = new TolkienCharacter("Merry", 36, HOBBIT);
40+
41+
final TolkienCharacter pippin = new TolkienCharacter("Pippin", 28, HOBBIT);
42+
43+
final TolkienCharacter gandalf = new TolkienCharacter("Gandalf", 2020, MAIA);
44+
45+
final TolkienCharacter gimli = new TolkienCharacter("Gimli", 139, DWARF);
46+
47+
final TolkienCharacter legolas = new TolkienCharacter("Legolas", 1000, ELF);
48+
49+
final TolkienCharacter aragorn = new TolkienCharacter("Aragorn", 87, MAN);
50+
51+
final TolkienCharacter boromir = new TolkienCharacter("Boromir", 37, MAN);
52+
53+
final TolkienCharacter sauron = new TolkienCharacter("Sauron", 50000, MAIA);
54+
55+
final TolkienCharacter galadriel = new TolkienCharacter("Galadriel", 3000, ELF);
56+
57+
final TolkienCharacter elrond = new TolkienCharacter("Elrond", 3000, ELF);
58+
59+
final TolkienCharacter guruk = new TolkienCharacter("Guruk", 20, ORC);
60+
61+
62+
final Movie theFellowshipOfTheRing = new Movie("the fellowship of the Ring", new Date(), "178 min");
63+
64+
final Movie theTwoTowers = new Movie("the two Towers", new Date(), "179 min");
65+
66+
final Movie theReturnOfTheKing = new Movie("the Return of the King", new Date(), "201 min");
67+
68+
69+
public List<TolkienCharacter> getFellowship() {
70+
71+
final List<TolkienCharacter> fellowshipOfTheRing = new ArrayList<>();
72+
73+
74+
// let's do some team building :)
75+
fellowshipOfTheRing.add(frodo);
76+
fellowshipOfTheRing.add(sam);
77+
fellowshipOfTheRing.add(merry);
78+
fellowshipOfTheRing.add(pippin);
79+
fellowshipOfTheRing.add(gandalf);
80+
fellowshipOfTheRing.add(legolas);
81+
fellowshipOfTheRing.add(gimli);
82+
fellowshipOfTheRing.add(aragorn);
83+
fellowshipOfTheRing.add(boromir);
84+
return fellowshipOfTheRing;
85+
}
86+
87+
public List<TolkienCharacter> getOrcsWithHobbitPrisoners() {
88+
final List<TolkienCharacter> orcsWithHobbitPrisoners = new ArrayList<TolkienCharacter>();
89+
orcsWithHobbitPrisoners.add(guruk);
90+
orcsWithHobbitPrisoners.add(merry);
91+
orcsWithHobbitPrisoners.add(pippin);
92+
return orcsWithHobbitPrisoners;
93+
}
94+
95+
public TolkienCharacter getFellowshipCharacter(String name) {
96+
List<TolkienCharacter> list = getFellowship();
97+
// as of API level 24
98+
// return list.stream().filter(s-> s.equals(name)).findFirst().get();
99+
for (TolkienCharacter t:list) {
100+
if (t.getName().equals(name)){
101+
return t;
102+
}
103+
}
104+
return null;
105+
}
106+
107+
public Map<Ring, TolkienCharacter> getRingBearers() {
108+
109+
Map<Ring, TolkienCharacter> ringBearers = new HashMap<>();
110+
111+
// ring bearers
112+
ringBearers.put(Ring.nenya, galadriel);
113+
ringBearers.put(Ring.narya, gandalf);
114+
ringBearers.put(Ring.vilya, elrond);
115+
ringBearers.put(Ring.oneRing, frodo);
116+
return ringBearers;
117+
}
118+
}

0 commit comments

Comments
 (0)