レルム用のJunitテストを作成しようとしましたが、正しく設定するのが難しいようです。私はそれを非同期的に走らせるべきかどうか分からない。実行している場合AndroidでJunitテストレルムLooper.prepare()を呼び出していないスレッド内でハンドラを作成できません
私は今人気のエラーを取得:
05-26 16:03:32.561 14131-14148/? I/TestRunner: started: aCreateNewPlayer(com.example.gideonsassoon.avariel.RealmTest)
05-26 16:03:32.567 14131-14148/? E/Exception: RealmException Can't create handler inside thread that has not called Looper.prepare()
05-26 16:03:32.568 14131-14148/? W/System.err: at com.example.gideonsassoon.avariel.database.RealmManager.<init>(RealmManager.java:20)
05-26 16:03:32.568 14131-14148/? W/System.err: at com.example.gideonsassoon.avariel.RealmTest.aCreateNewPlayer(RealmTest.java:40)
05-26 16:03:32.569 14131-14148/? I/TestRunner: finished: aCreateNewPlayer(com.example.gideonsassoon.avariel.RealmTest)
は、これがホストし、すべてを実行し、私のRealmManagerクラスのコードです。
package com.example.gideonsassoon.avariel.database;
import android.app.Activity;
import android.os.Bundle;
import com.example.gideonsassoon.avariel.datamodels.Player;
import com.example.gideonsassoon.avariel.ui.MainFragmentActivity;
import com.facebook.stetho.Stetho;
import com.uphyca.stetho_realm.RealmInspectorModulesProvider;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmQuery;
import io.realm.RealmResults;
/**
* Created by Gideon Sassoon on 16/05/2017.
*/
public class RealmManager extends Activity {
public static final String TAG = MainFragmentActivity.class.getName();
private Realm realm;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Looper.prepare();
realm = Realm.getDefaultInstance();
/**
* https://realm.io/docs/java/latest/#getting-started
* http://facebook.github.io/stetho/
* https://github.com/uPhyca/stetho-realm
* chrome://inspect/#devices
*/
Stetho.initializeWithDefaults(this);
Realm.init(this);
Stetho.initialize(
Stetho.newInitializerBuilder(this)
.enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
.enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
.build());
super.onCreate(savedInstanceState);
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
@Override
protected void onDestroy() {
super.onDestroy();
realm.close();
}
public void savePlayer(Player player) {
Player player2 = realm.copyToRealm((player));
}
public void queryPlayer(String key, String value){
// https://realm.io/docs/java/latest/#queries
RealmQuery<Player> query = realm.where(Player.class);
query.equalTo("playerName","Gideon");
query.or().equalTo(key, value);
RealmResults<Player> result1 = query.findAll();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Player player = realm.createObject(Player.class);
player.setCurrentHP(26);
player.setAlignment("Lawful Evil");
}
});
//Player player = result1.where().equalTo(key, value).or().equalTo("playerName", "Gideon").findFirst();
}
}
これは、あなたが何らかの理由でプロジェクトの残りの部分を見たい場合はそのコード
package com.example.gideonsassoon.avariel;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;
import com.example.gideonsassoon.avariel.database.RealmManager;
import com.example.gideonsassoon.avariel.datamodels.Player;
import junit.framework.TestCase;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
/**
* Created by Gideon Sassoon on 21/05/2017.
*/
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RealmTest extends TestCase {
RealmManager realmManager;
Player player;
Player actualPlayerResult;
String actualStringResult;
long actualLongResult;
@BeforeClass
public static void setup() {
System.out.println("Before Start /r/n, Welcome to the Doom Room!");
Log.i("TEST_START", "Before Start /r/n, Welcome to the Doom Room!");
}
@Test
public void aCreateNewPlayer() {
try {
realmManager = new RealmManager();
player = new Player();
player = player.create(01, "Gideon", "Barlock", "Orc", "Lawful Evil", "Fighter", "A wandering Warrior", 0, 30, 0, null, null, null, null, null, null, null, null, null, 0);
realmManager.savePlayer(player);
Log.i("REALM", realmManager.toString());
} catch (Exception e) {
// Sep 12, 2013 6:50:17 AM
Log.e("Exception", "RealmException " + String.valueOf(e.getMessage()));
e.printStackTrace();
}
}
@Test
public void bGetPlayer() {
//actualPlayerResult = Result here;
realmManager.queryPlayer("playerName", "Gideon");
Log.i("REALM Test getPlayer", "Our player's name is: " + actualPlayerResult.getName());
}
public void cPlacePlayerInApp() {
}
}
を実行し、私のJUnitテストクラスです。
realmManager = new RealmManager(); //won't work because RealmManager is an Activity
ながら活動、フラグメント、および:あなたはここで、このラインでコンストラクタを呼び出すことによって活動(RealmManager
)のサブクラスを初期化しているため
You are more than welcome to do so here :)
あなたのお役に立てれば答えを受け入れることができますか? –