42

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????

Brian White
  • 8,332
  • 2
  • 43
  • 67
cointreau
  • 864
  • 1
  • 10
  • 21

15 Answers15

63

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

Shangeeth Sivan
  • 2,150
  • 1
  • 20
  • 20
24

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.

CLIFFORD P Y
  • 16,974
  • 6
  • 30
  • 45
  • Thanks, exactly my case. I would add that links compilation errors were actually presented in the logs above. – Leo DroidCoder Sep 01 '21 at 09:10
  • Thanks for this! In my case, it was a compiler error somewhere else in that module's test code, unrelated to the test I was trying to run. – hustoj2 Nov 23 '21 at 15:38
  • 1
    Generally the log console of the running test shows what's wrong. In my case there was also a compiler error but I didn't scroll until the end of the log console... that's a learnt lesson for me. Thanks for your comment. – aguiarroney Feb 09 '22 at 13:26
21

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.

Brian White
  • 8,332
  • 2
  • 43
  • 67
  • I tried your suggestion and now I can see the message 'No tests found for given includes', though my test code is same above. – cointreau May 27 '15 at 06:51
  • 1
    At least it's a different problem so we're making progress. I believe your test is on the wrong directory. I have mine in `src/test/java/...` (i.e. parallel to _main_). `src/androidTest/java` is for tests run on the device itself. – Brian White May 27 '15 at 12:45
  • 4
    In the report the unit tests were successful but AS said test events were not received. I just disabled "Use in-process build" according to your advice, and now I see the result in AS. I freaking hate this kind of things in tools. Why can't they just work as expected out of the box, without user's spending hours for unknown errors and then finally searching the web for a fix. – Damn Vegetables Jun 05 '15 at 02:55
  • 2
    As I mentioned earlier, it works, but I have a strange issue. In the report, the unit tests seems to have run once. But in AS, there are "Gradle Test Executor 1" and "Gradle Test Executor 2" running the same tests twice each. This is not a serious problem for me, but can I fix this? – Damn Vegetables Jun 05 '15 at 03:09
  • 1
    @SinJeong-hun I just tried this on Android Studio 1.2.2, it is indeed only the "in-process build" option that makes the difference. The `--rerun-tasks` switch didn't change anything. I feel your pain, instead of doing the actual work I'm forced to deal with this stuff. – Malcolm Jun 16 '15 at 02:46
  • @Malcolm this is weird, I'm on AS 1.2.2 as well and disabling "in-process build" didn't change anything for me, but adding "--rerun-tasks" did the trick. – Suau Jun 24 '15 at 09:45
  • After spending 4 hours fighting this with Android Studio 1.2.2 on my Mac, disabling in-process build fixed the issue. – Bradford2000 Jul 10 '15 at 19:29
  • FIxed the issue, but now my tests seem to be running twice. Better than none at all, I suppose – BeccaP Jul 30 '15 at 17:18
  • Thank you, this helped me. Wasted a days development finding out what the problem was. Obviously productivity belongs in other languages and environments. – Phil Aug 15 '15 at 11:14
  • 8
    Hey guys where is this option in the current Android Studio? – BabyishTank Sep 03 '21 at 19:17
3

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.

Zheko
  • 673
  • 6
  • 16
3

On my side I fixed the issue by using:

import org.junit.jupiter.api.Test

instead of

import org.junit.Test

Yasser AKBBACH
  • 539
  • 5
  • 7
2

In my case (AS Chipmunk), I started receiving this error after trying to modify the run configurations for the tests (to stop code coverage from showing the Hilt generated classes).

What I did was delete the .idea folder and after restarting Android Studio tests were working once again.

1

Try to build the project and run tests again. In my case I had a bug in the code and therefore the tests did not run either.

Vytenis Bučius
  • 147
  • 1
  • 4
0

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
GoRoS
  • 5,183
  • 2
  • 43
  • 66
0

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.

Chetan Narsude
  • 311
  • 1
  • 5
0

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

Sebastian Corradi
  • 1,353
  • 2
  • 14
  • 25
0

Do double check if you are in gradle offline mode. A stupid but pretty common mistake.

S.Javed
  • 383
  • 3
  • 8
0

This is an old issue, but for future google searchers and perhaps interested parties that could work around this, it is worth mentioning that if you are using one of the more popular walk-throughs to set up JaCoCo + Robolectric + Espresso - https://medium.com/@rafael_toledo/setting-up-an-unified-coverage-report-in-android-with-jacoco-robolectric-and-espresso-ffe239aaf3fa . Please add below this:

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
    jacoco.excludes = ['jdk.internal.*']
}
0

I had a similar message with instrumented tests. Turned out, you have to run connectedAndroidTest for them to be run.

Eyjafl
  • 1,046
  • 6
  • 14
0

This happens when some of the unit test cases fail. Try to run

./gradlew clean testDebugUnitTest 

to get the failing unit tests.

hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38
0

This problem seems to crop up in different ways over the years. Here is what fixed it for me in 2023 with Studio 2022.2.1 Patch 2.

Tests in module projects were not running and "Tests not recieved" error. Added this to the module's gradle to fix:

android {
    namespace = "com.foo.bar"
    testOptions {
         unitTests {
            all {
                 it.enabled = true
            }
        }
    }
 }
bsautner
  • 4,479
  • 1
  • 36
  • 50