From 5a382baab4e3bb496fd00ab4f73450b57279ea2d Mon Sep 17 00:00:00 2001 From: Mary Tran Date: Mon, 8 Oct 2018 16:25:15 -0400 Subject: [PATCH] Running unit tests with Robolectric instead of starting up emulator every time --- OlympusServices/app/build.gradle | 11 +++++++++- .../olympus/olympusservices/DBHelper.java | 7 +++++++ .../olympus/olympusservices/DBUnitTest.java} | 21 +++++++++++-------- 3 files changed, 29 insertions(+), 10 deletions(-) rename OlympusServices/app/src/{androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java => test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java} (82%) diff --git a/OlympusServices/app/build.gradle b/OlympusServices/app/build.gradle index 4a8aefa..481e4c8 100644 --- a/OlympusServices/app/build.gradle +++ b/OlympusServices/app/build.gradle @@ -16,6 +16,11 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + testOptions { + unitTests { + includeAndroidResources = true + } + } } dependencies { @@ -24,10 +29,14 @@ dependencies { implementation 'com.android.support:design:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' + testImplementation 'org.robolectric:robolectric:4.0-alpha-3-SNAPSHOT' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'com.jaredrummler:material-spinner:1.2.5' implementation 'com.android.support:design:28.0.0-alpha3' implementation 'com.rengwuxian.materialedittext:library:2.1.4' - } + +repositories { + maven { url "https://oss.sonatype.org/content/repositories/snapshots" } +} \ No newline at end of file diff --git a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java index 2086573..9c7115e 100644 --- a/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java +++ b/OlympusServices/app/src/main/java/com/uottawa/olympus/olympusservices/DBHelper.java @@ -6,6 +6,13 @@ import android.database.sqlite.SQLiteOpenHelper; import android.database.Cursor; import android.content.ContentValues; +/** + * The class DBHelper allows the Android application to access and perform + * CRUD operations on the tables of the SQLite database. + * There is currently a table of all users' login information. + * + * To use, create an object of this class with the current activity as context. + */ public class DBHelper extends SQLiteOpenHelper { diff --git a/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java similarity index 82% rename from OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java rename to OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java index c372bd8..283b933 100644 --- a/OlympusServices/app/src/androidTest/java/com/uottawa/olympus/olympusservices/DBTest.java +++ b/OlympusServices/app/src/test/java/com/uottawa/olympus/olympusservices/DBUnitTest.java @@ -3,17 +3,20 @@ package com.uottawa.olympus.olympusservices; import android.content.Context; import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; -import android.support.test.InstrumentationRegistry; -import android.support.test.runner.AndroidJUnit4; + import org.junit.Test; import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; import static org.junit.Assert.*; -@RunWith(AndroidJUnit4.class) -public class DBTest { - private DBHelper dbHelper = new DBHelper(InstrumentationRegistry.getTargetContext()); +@RunWith(RobolectricTestRunner.class) +@Config(packageName = "com.uottawa.olympus.olympusservices") +public class DBUnitTest { + private DBHelper dbHelper = new DBHelper(RuntimeEnvironment.application); @Test public void testAddAndDeleteUser(){ @@ -54,13 +57,13 @@ public class DBTest { deleted = dbHelper.deleteUser("admin"); - assertEquals(true, deleted); + assertTrue(deleted); deleted = dbHelper.deleteUser("mgarzon"); - assertEquals(true, deleted); + assertTrue(deleted); deleted = dbHelper.deleteUser("jguzman"); - assertEquals(true, deleted); + assertTrue(deleted); } @@ -71,7 +74,7 @@ public class DBTest { added = dbHelper.addUser(new User("mgarzon", "soccer", "Miguel", "Garzon")); assertTrue(added); - added = dbHelper.addUser(new ServiceProvider("mgarzon", "soccer", "Miguel", "Garzon")); + added = dbHelper.addUser(new ServiceProvider("mgarzon", "seg2105", "Juan", "Guzman")); assertTrue(!added); dbHelper.deleteUser("mgarzon");