gpt4 book ai didi

java - Android Studio 测试不工作

转载 作者:行者123 更新时间:2023-12-02 11:57:25 25 4
gpt4 key购买 nike

当 Android Studio 无缘无故开始报告“未找到测试”时,我该怎么办?昨天的测试有效。现在我得到了这个随机错误。

存储库是 available online 。我的实际测试类如下所示:

package dev.game.adventure;

import static junit.framework.Assert.assertEquals;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.*;
import static org.mockito.Mockito.*;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.test.InstrumentationRegistry;
import android.widget.TableLayout;
import android.widget.TextView;


@RunWith(MockitoJUnitRunner.class)
public class AdventureTest {

private static final String FAKE_STRING = "HELLO WORLD";

@Mock
Simulation engine;

@Mock
AdventureWorld mWorld;

@Mock
Adventure mworld;

@Mock
Context aContext;

@Mock
FullscreenActivity mActivity;

@Mock
Drawable mDrawable;

@Mock
Resources mResources;

@Mock
Place mPlace;

@Mock
AdventureGame ag;



@Test
@Ignore
public void Tedye(){
//when(mWorld.defaultPlace()).thenReturn(mPlace);
// myObjectUnderTest.wakeMeAfter(new WalkingPerson(myObjectUnderTest, mWorld, "new", 2, mActivity), 10);
//String result2 = myObjectUnderTest.getHelloWorldString();
//assertThat(result2, is(FAKE_STRING));} createPlace("Heaven", target, R.mipmap.dungeon2);
// Adventure a = new Adventure(textview, mactivity, ag);

}


@Test
@Ignore
public void testd() {
// Textview scrollable = ''''''''''''(R.id.textView1);

when(mWorld.defaultPlace()).thenReturn(mPlace);
Context mCont;

}




@Test
public void adventureWorld() {
// Simulation myObjecUnderTest = new Simulation();
Adventure a = new Adventure(new TextView(aContext), mActivity, ag);
Player p = a.getPlayer();
p.say("foobar", mActivity);
p.say("Hello my name is " + a.getPlayer().getMe().name, mActivity);

}

@Test
public void adventureTest () {
Simulation myObjectUnderTest = new Simulation();
when(mWorld.defaultPlace()).thenReturn(mPlace);
myObjectUnderTest.wakeMeAfter(new WalkingPerson(myObjectUnderTest, mWorld, "new", 2, mActivity), 10);
String result2 = myObjectUnderTest.getHelloWorldString();
assertThat(result2, is(FAKE_STRING));
}

@Test
public void personTest () {
Simulation myObjectUnderTest = new Simulation();
when(mResources.getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
when(mActivity.getResources()).thenReturn(mResources);
when(mActivity.getResources().getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
when(mWorld.defaultPlace()).thenReturn(mPlace);
WalkingPerson myObjectUnderTest2 = new WalkingPerson(myObjectUnderTest, mWorld, "blaha", 2, mActivity);
String result2 = myObjectUnderTest2.getHelloWorldString();
myObjectUnderTest2.getThings();
myObjectUnderTest2.getWorld();
assertThat(result2, is(FAKE_STRING));
}

@Test
public void trollTest () {
Simulation myObjectUnderTest = new Simulation();
AdventureWorld ag;
when(mResources.getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
when(mActivity.getResources()).thenReturn(mResources);
when(mActivity.getResources().getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
when(mWorld.defaultPlace()).thenReturn(mPlace);
WalkingPerson myObjectUnderTest2 = new Troll(myObjectUnderTest, mWorld, "blaha", mActivity);
String result2 = myObjectUnderTest2.getHelloWorldString();
myObjectUnderTest2.getThings();
AdventureWorld adv = (AdventureWorld) myObjectUnderTest2.getWorld();
//assertThat(adv.defaultPlace().toString().equals(mWorld.defaultPlace().toString()));
// assertThat(adv.defaultPlace(), is(FAKE_STRING));
assertThat(myObjectUnderTest2.getName(), is("Loke"));
assertThat(adv.messsage, is(FAKE_STRING));
}

@Test
public void cokeTest () {
when(mWorld.getPlace("Dungeon")).thenReturn(mPlace);
mWorld.getPlace("Dungeon").addThing(new CocaCola("Ljummen cola"));
Simulation myObjectUnderTest = new Simulation();
when(mResources.getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
when(mActivity.getResources()).thenReturn(mResources);
when(mActivity.getResources().getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
when(mWorld.defaultPlace()).thenReturn(mPlace);
WalkingPerson myObjectUnderTest2 = new Troll(myObjectUnderTest, mWorld, "blaha", mActivity);
String result2 = myObjectUnderTest2.getHelloWorldString();
myObjectUnderTest2.getThings();
myObjectUnderTest2.getWorld();
assertThat(result2, is(FAKE_STRING));
}

@Test
public void testPlace () {
Simulation myObjectUnderTest = new Simulation();
when(mResources.getDrawable(R.mipmap.dungeon)).thenReturn(mDrawable);
mWorld.createPlace("Heaven", mActivity, R.mipmap.dungeon2);
mWorld.createPlace("Hell", mActivity, R.mipmap.dungeon2);
mWorld.connect("Heaven", "Hell", "Down", "Up");
mWorld.randomPlace();

}

@Test
public void useAppContext () throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("dev.game.adventure", appContext.getPackageName());
}
}

enter image description here

构建日志:https://travis-ci.org/montao/adventure

最佳答案

我无法解释为什么您的测试似乎之前运行而不是现在运行,但我克隆了该项目并能够通过以下方式运行测试:

  • AdventureTestsrc/androidTest 源代码树移动到 src/test
  • AdventureTest 中注释掉/删除所有 Android 类
  • 添加一个简单的虚拟 @Test 方法(例如 assertTrue(false))

一般经验法则:

  • 如果您要在测试中使用 Android 类,请使用 Robolectric
  • 否则,请使用您喜欢的任何测试运行器实现(在您的情况下为 Mockito)

编辑 - 添加示例 ROBOLECTRIC 测试

Robolectric 可让您在普通 JVM 上测试 Android 代码,而不必在 Dalvik 或其他平台上运行。下面是几行内容可以帮助您入门,但请参阅 http://robolectric.org/因为该框架的产品非常广泛。

app/build.gradle中,添加以下内容:

android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}

dependencies {
...
testCompile 'org.assertj:assertj-core:3.8.0'
testImplementation "org.robolectric:robolectric:3.5.1"
...
}

在下面的测试中,FullScreenActivity 通过其 create() 方法进行实例化和驱动,然后用作 Context 来创建 TextView 。然后调用您自己的一些应用程序代码,最后针对 TextView 运行断言以验证某些预期状态。没有模拟 - 只是在工作站的 JVM 上运行的常规 Android 类。

@RunWith(RobolectricTestRunner.class)
public class AdventureTest {
...
@Test
public void adventureWorld() {
mActivity = Robolectric.buildActivity(FullscreenActivity.class)
.create()
.get();

ag = new AdventureGame(mActivity, null);

// Simulation myObjecUnderTest = new Simulation();
final TextView speechTextView = new TextView(mActivity);

final Adventure adventure = new Adventure(speechTextView, mActivity, ag);

final Player player = adventure.getPlayer();
player.say("foobar", mActivity);
player.say("Hello my name is " + adventure.getPlayer().getMe().name, mActivity);


assertThat(speechTextView.getText()).containsIgnoringCase("Hello my name is");
}
...

希望有帮助!

关于java - Android Studio 测试不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47503707/

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