Reputation: 26652
What should I do when Android Studio for no reason starts reporting "No tests were found" ? The tests worked yesterday. Now I get this random error instead.
The repository is available online. My actual test klass looks like:
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());
}
}
The build log: https://travis-ci.org/montao/adventure
Upvotes: 0
Views: 2575
Reputation: 3569
i can't speak to why your tests seem to run before and not now, but i cloned the project and was able to run a test by:
AdventureTest
from the src/androidTest
source tree into src/test
AdventureTest
assertTrue(false)
)general rule of thumb:
EDIT - ADDING SAMPLE ROBOLECTRIC TEST
Robolectric lets you test your Android code on a plain JVM rather than having to run on Dalvik or otherwise. below is a few lines to get you started, but please refer to http://robolectric.org/ as the framework's offerings are quite expansive.
in app/build.gradle
, add the following:
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
...
testCompile 'org.assertj:assertj-core:3.8.0'
testImplementation "org.robolectric:robolectric:3.5.1"
...
}
in the below test, FullScreenActivity
is instantiated and driven through its create()
method and is then used as a Context
to create a TextView
. a bit of your own application code is then called, and then finally an assertion is run against the TextView
to verify some expected state. no mocks - just what seems to be regular Android classes running on your workstation's JVM.
@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");
}
...
i hope that helps!
Upvotes: 2