gpt4 book ai didi

java - Android 上的 Junit 测试 Realm 。无法在未调用 Looper.prepare() 的线程内创建处理程序

转载 作者:行者123 更新时间:2023-11-29 02:40:56 24 4
gpt4 key购买 nike

尝试为 Realm 创建 Junit 测试,但我似乎在正确设置它时遇到了一些困难。我不确定我是否应该异步运行它,我的直觉说不。

运行时我遇到了一个非常流行的错误:

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();
}
}

这是运行该代码的我的 Junit 测试类

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() {


}
}

如果您出于某种原因想要查看项目的其余部分。

You are more than welcome to do so here :)

最佳答案

您收到该错误的原因是因为您正在通过在此处调用此行中的构造函数来初始化 Activity (RealmManager) 的子类:

realmManager = new RealmManager(); //won't work because RealmManager is an Activity

虽然 Activity、Fragment 和 Service 具有无参数构造函数,但它们被设计为由操作系统实例化,仅调用构造函数不足以获得功能齐全的实例。

现在编写插桩单元测试(即在 Android 手机上运行的单元测试)的正确用法如下:

@RunWith(AndroidJUnit4.class)
public class RealmManagerTest { //no need to extend TestCase here!!!!

public ActivityTestRule<RealmManager> activityRule = new ActivityTestRule<>(RealmManager.class);

RealmManager realmManager;

@Before
public void setUp() throws Exception {
activityRule.launchActivity(null);
realmManager = activityRule.getActivity();
}

ActivityRule 会正确地为你实例化 Activity,onCreate()onResume() 的回调会被执行,Looper 会按要求准备好.

请注意,您不再需要扩展 TestCase。请看the official docs有关仪表化单元测试的更多信息。

关于java - Android 上的 Junit 测试 Realm 。无法在未调用 Looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44204958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com