jeudi 28 juillet 2016

Junit test class in android studio

I'm trying to create a LOCAL JUNIT Test class for my database-class in Android. However I'm running into a couple of problems. This is an example of the test-class I want to create:

import android.test.AndroidTestCase;
import android.test.RenamingDelegatingContext;
import com.example.c.readit.model.Book;
import com.example.c.readit.model.VolumeInfo;
import com.example.c.readit.sqlite.SqliteHelper;
import org.junit.Test;

public class TestDB extends AndroidTestCase{
    private SqliteHelper db;
@Override
public void setUp() throws Exception {
    super.setUp();
    RenamingDelegatingContext context = new RenamingDelegatingContext(getContext(), "test_");
    db = new SqliteHelper(context);
}

@Override
public void tearDown() throws Exception {
    db.close();
    super.tearDown();
}

@Test
public void testSaveBook(){

    String test_ID = "1234";
    String test_TITLE = "TestBook";
    String test_DESCRIPTION = "TestDescription";
    String test_PUBLISHER = "TestPublisher";
    String test_READ = "yes";

    VolumeInfo testVolumeInfo = new VolumeInfo(test_TITLE,test_PUBLISHER,test_DESCRIPTION);
    Book testBook = new Book(test_ID,testVolumeInfo,test_READ);

    long wasSuccesful = db.saveBookMyBooks(testBook);

    assertTrue(wasSuccesful != -1);
}

}

However, a couple of the methods are deprecated (since API 24). When looking them up in the Android documentation I can see that we're supposed to use the 'Testing Support Library' (http://ift.tt/2a3ba50).

I am having a hard time finding examples to learn how to work with this. When I find a working example, I'm running into the problem that I cannot get the context for my SqlLite DB.How can I get the context Some examples are available for the instrumented test classes, but I want to write a local JUNIT test-class.

My gradle build:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.example.c.readit"
        minSdkVersion 14
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        // To fix 'Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (24.0.0) and test app (23.0.1) differ. See http://ift.tt/1ZPcybz for details.'
        resolutionStrategy.force 'com.android.support:support-annotations:23.0.1'
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.google.code.gson:gson:2.6.2'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    androidTestCompile 'com.android.support.test:runner:0.4'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.4'
}

Thanks

Aucun commentaire:

Enregistrer un commentaire