Running unit tests with Robolectric instead of starting up emulator every time

This commit is contained in:
Mary Tran 2018-10-08 16:25:15 -04:00
parent 8ecd089fe3
commit 5a382baab4
3 changed files with 29 additions and 10 deletions

View file

@ -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" }
}

View file

@ -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 {

View file

@ -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");