'Why does Android Studio say "Test events were not received"?

I'm trying to unit test in my android application, and this is the simple test tutorial what i'm doing.

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ServerListManagerTest extends AndroidTestCase{

   @Test
   public void testTrueIsTrue() throws Exception {
    assertEquals(true, true);
    }
}

The directory is like this, src\main\androidTest\java\some packages\ServerListManagerTest.java

I tried changing directory of this, and also build configuration. but android studio still doesn't recognize my unit test though build was successful.

This is my build.gradle in app,

apply plugin: 'com.android.application'

android {
   compileSdkVersion 21
   buildToolsVersion "21.1.2"

   defaultConfig {
       applicationId "com.kaist.se.pmpapp"
       minSdkVersion 16
       targetSdkVersion 21
       versionCode 1
       versionName "1.0"
   }

buildTypes {
       release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
      }
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } }



dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    androidTestCompile 'org.robolectric:robolectric:2.4'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile group: 'junit', name: 'junit', version: '4.12'
  }

What's wrong in my code????



Solution 1:[1]

I assume you're using Android Studio version 1.2, the latest at this time.

I don't think anything is wrong with your code. According to Jason Atwood's post, the problem seems related to gradle caching the previous results and not running it again. If you look at the "Gradle console", you'll see everything say "UP-TO-DATE". However, his suggestion of adding the "--rerun-tasks" option to the script parameters was not sufficient for me.

In addition to "--rerun-tasks", I had to turn off the in-process build and force it to call the external gradlew tool. To do this, go to...

File > Settings > Build, Execution, Deployment > Compiler

Then un-check the "Use in-process build" option. Hopefully a future release of Android Studio will fix this and we can re-enable that option.

Solution 2:[2]

Sometimes you might have simple a compiler issue and end up with this message. This happened to me.. The compiler issue was not pointed out directly had to scroll down on the stack trace to find what is the issue.

enter image description here

Solution 3:[3]

I faced the same issue Test events were not received , I checked the function I wrote and found that there is a compiler error.

My solution is check the files/functions you are testing there may be compiler errors

this may help some one with same problem.

Solution 4:[4]

If you have multiple Flavours, you may have selected another flavour, not the one with your tests. E.g. if you have prod and debug flavour and your tests are in debug, you may have switched to prod one and then you'll get the same error. Selecting the debug flavour will resolve the issue.

Solution 5:[5]

In my case, apart from fixing compiling issues, none of the answers worked. However, invalidating Android Studio's cache did the trick.

  1. Go to File
  2. Press Invalidate Caches...
  3. Click on Invalidate and Restart

Solution 6:[6]

In my case, the problem was that one of the test methods I had written was missing public from the method declaration. Like

@Test
void testSomething() {
   //
}

as opposed to

@Test
public void testSomething() {
}

Interestingly even though I was trying to debug some other properly defined method in the same class, the framework would not find any of the tests in the class.

So it's not just the syntax errors, but the semantic issues wrt the unit test framework also throw everything off.

Solution 7:[7]

In My case the solution was to go to androidStudio -> Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle and then, change Gradle JDK (From 15 to 13)

Regards

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Shangeeth Sivan
Solution 3
Solution 4 Zheko
Solution 5 GoRoS
Solution 6 Chetan Narsude
Solution 7 Sebastian Corradi