nhachicha / SnappyDB

A key-value database for Android
1.78k stars 220 forks source link

Error while testing #10

Open amelilie opened 10 years ago

amelilie commented 10 years ago

Hello,

I use SnappyDB in my android project, it works well. But when I want to test it (with robolectric) I have 2 errors : java.lang.NoClassDefFoundError: Could not initialize class com.snappydb.DBFactory java.lang.UnsatisfiedLinkError: no snappydb-native in java.library.path

In my build.gradle I have the same dependencies that the README. I don't find the problem...

nhachicha commented 10 years ago

Hi, could you pls send me the project so I can reproduce ?

amelilie commented 10 years ago

I can't send it, but I can describe it.

The structure of the project : src/main/java/fr.project.myproject src/test/java

The class which uses Snappy :

public class CacheManager {

private static CacheManager sInstance;

private DB mSnappydb;

private Context mContext;

public static CacheManager getInstance(){
    if (sInstance == null){
        sInstance = new CacheManager();
    }
    return sInstance;
}

private CacheManager(){
}

public void init(final Context context){
    this.mContext = context;
    //open (or create) the database
    try {
        mSnappydb = DBFactory.open(context);
    } catch (SnappydbException e) {
        e.printStackTrace();
    }
}

public void start(){
    //open (or create) the database
    try {
        mSnappydb = DBFactory.open(mContext);
    } catch (SnappydbException e) {
        e.printStackTrace();
    }
}

public void stop(){
    //close the db
    try {
        CacheManager.getInstance().getSnappydb().close();
    } catch (SnappydbException e) {
        e.printStackTrace();
    }
}

public void loadHome(){
    try {
        Log.d(">>>>>>>>>", "cache");

        HomeData home = mSnappydb.get(WSConfig.HOME_URL, HomeData.class);
        DataManager.getInstance().receiveData(home);

    } catch (SnappydbException e) {
        Log.e("CacheManaer loadHome :", e.getMessage());
        e.printStackTrace();
    }
}

public void saveHome(HomeData home){
    try {
        mSnappydb.put(WSConfig.HOME_URL, home);

        Log.d(">>>>>>>>>", "save home in Snappy");

    } catch (SnappydbException e) {
        Log.e("CacheManager saveHome :", e.getMessage());
        e.printStackTrace();
    }
}

public DB getSnappydb() {
    return mSnappydb;
}

}

(init()/start()/stop() call in the Application) I call this class with : CacheManager.getInstance().loadHome();

build.gradle :


buildscript { repositories { mavenCentral() maven { url 'http://download.crashlytics.com/maven' } }

dependencies {
    classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
    classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
    classpath 'com.nabilhachicha:android-native-dependencies:0.1+'
}

} apply plugin: 'com.android.application' apply plugin: 'crashlytics' apply plugin: 'robolectric' apply plugin: 'android-native-dependencies'

repositories { maven { url 'http://download.crashlytics.com/maven' } }

android { compileSdkVersion 19 buildToolsVersion '19.1'

defaultConfig {
    applicationId "fr.project.myproject"
    minSdkVersion 15
    targetSdkVersion 19
}
buildTypes {
    debug {

    }

    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

sourceSets{
    androidTest.setRoot('src/test')
}

}

native_dependencies { artifact 'com.snappydb:snappydb-native:0.2.+' }

dependencies { compile fileTree(dir: 'libs', include: ['*.jar'])

// Support libs
compile 'com.android.support:support-v4:19.+'

compile 'de.greenrobot:eventbus:2.2.+'

// JSON libs
compile 'com.google.code.gson:gson:2.2.+'

// Annotation libs
compile 'com.jakewharton:butterknife:5.1.+'

// Image libs
compile 'com.nostra13.universalimageloader:universal-image-loader:+'

compile files('libs/volley.jar')
compile 'com.snappydb:snappydb-api:0.2.+'

compile files('libs/twitter4j-4.0.2.zip')

compile 'com.crashlytics.android:crashlytics:1.+'

androidTestCompile 'junit:junit:4.10'
androidTestCompile 'org.robolectric:robolectric:2.3'

}

// Sonar management File sonar = file(sonarScriptPath) if(sonar.canRead()){ apply from: sonar.absolutePath }

// Publish management File publish = file(publishScriptPath) if(publish.canRead()){ apply from: publish.absolutePath }


My tests : HomeActivity isn't null ; the object created is the same than the object in the DB ; and other simple things like that...

VladSumtsov commented 9 years ago

I have the same issue. How can I fix it?

nhachicha commented 9 years ago

Hi @VladSumtsov are you still using native_dependencies plugin? By now you should start using the new AAR format (I developed this plugin because at the time AAR format didn't handle native libs), can you please try it

dependencies {
    compile 'com.snappydb:snappydb-lib:0.5.0'
    compile 'com.esotericsoftware.kryo:kryo:2.24.0'
}

Cheers

VladSumtsov commented 9 years ago

Hi @nhachicha I use aar plugin, as you described. As I understood, robolectric doesn't work with native dependencies. So, I get java.lang.UnsatisfiedLinkError: no snappydb-native in java.library.path error when try to run test. Do you have any solution, how can we test snappy with robolectric?

nhachicha commented 9 years ago

Hi @VladSumtsov

Yep, unfortunately Robolectric doesn't support native library. you can use however regular Android instrumentation test, example

Cheers,

luckcoolla commented 8 years ago

Seems like we have to wait here for another Robolectric issue fix: https://github.com/robolectric/robolectric/issues/1171