No tests found for given includes Error, when running Parameterized Unit test in Android Studio
Asked Answered
A

43

326

I have tried to run Parameterized Unit Tests in Android Studio, as shown below:

import android.test.suitebuilder.annotation.SmallTest;  

import junit.framework.TestCase;    

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;  

import java.util.Arrays;
import java.util.Collection;    

@RunWith(Parameterized.class)
@SmallTest
public class FibonacciTest extends TestCase {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
                {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, {5, 5}, {6, 8}
        });
    }   

    @Parameter // first data value (0) is default
    public /* NOT private */ int fInput;    

    @Parameter(value = 1)
    public /* NOT private */ int fExpected; 

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.calculate(fInput));
    }
}

The result is an error stating No Test Run. However, if I remove the Parameterized tests, and change them to individual tests, it works.

Can anyone shed some light on why this is not working? Ar Parameterized unit tests not supported in Android development yet?

Below is the error with stack trace:

FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:testDebug'.
> No tests found for given includes: [com.example.......FibonacciTest]
* Try:
Run with --info or --debug option to get more log output.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:testDebug'.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:64)
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58)
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:42)
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:53)
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(AbstractTask.java:310)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(AbstractTaskPlanExecutor.java:79)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:63)
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:51)
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:23)
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:88)
    at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.access$200(DefaultBuildExecuter.java:23)
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(DefaultBuildExecuter.java:68)
    at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:62)
    at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:55)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:149)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:90)
    at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:54)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:49)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
    at org.gradle.util.Swapper.swap(Swapper.java:38)
    at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.DaemonHealthTracker.execute(DaemonHealthTracker.java:47)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:66)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:71)
    at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.health.HintGCAfterBuild.execute(HintGCAfterBuild.java:41)
    at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120)
    at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
    at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:246)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
Caused by: org.gradle.api.GradleException: No tests found for given includes: [com.example........FibonacciTest]
    at org.gradle.api.internal.tasks.testing.NoMatchingTestsReporter.afterSuite(NoMatchingTestsReporter.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:87)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy46.afterSuite(Unknown Source)
    at org.gradle.api.internal.tasks.testing.results.TestListenerAdapter.completed(TestListenerAdapter.java:48)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:87)
    at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:31)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy45.completed(Unknown Source)
    at org.gradle.api.internal.tasks.testing.results.StateTrackingTestResultProcessor.completed(StateTrackingTestResultProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.results.AttachParentTestResultProcessor.completed(AttachParentTestResultProcessor.java:52)
    at org.gradle.api.internal.tasks.testing.processors.TestMainAction.run(TestMainAction.java:51)
    at org.gradle.api.internal.tasks.testing.detection.DefaultTestExecuter.execute(DefaultTestExecuter.java:75)
    at org.gradle.api.tasks.testing.Test.executeTests(Test.java:527)
    at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:75)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(AnnotationProcessingTaskFactory.java:226)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:219)
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(AnnotationProcessingTaskFactory.java:208)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:589)
    at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(AbstractTask.java:572)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:80)
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:61)
    ... 57 more
BUILD FAILED
Total time: 4.153 secs
No tests found for given includes: [com.example......FibonacciTest]
Acreinch answered 27/5, 2015 at 6:29 Comment(1)
Please try making your Test class public. The compiler may be unable to find your test classes if that's the case. Hope this helps.Zootomy
D
537

Add to your build.gradle:

test {
    useJUnitPlatform()
}
Dumbbell answered 1/8, 2019 at 13:6 Comment(8)
As a reference from the documentation docs.gradle.org/current/userguide/…. The code above enables JUnit5 Platform to run the tests in your JVM project. Based on the dependencies or configuration the new Jupiter engine will execute those tests or if you need backward compatibility you can inject/configure Vintage (JUnit4) engine will do it.Dominickdominie
In Gradle's Kotlin DSL: https://mcmap.net/q/100766/-how-do-i-use-the-native-junit-5-support-in-gradle-with-the-kotlin-dslHipparch
Adding reference from another answer, for additional info. linkLiu
This seems not enough in my caseVanillic
Silly 🙃 that this isn't taken care of when doing a migration from Maven to Gradle with gradle init. Another consideration is the order of test in build.gradle, as in where it appears in the file.Sociolinguistics
Note that this produces a bug when it's used inside the android target of a Kotlin Multi-platform project.Gagman
This is happing with me when I am both using useJUnitPlatform() useTestNG() in build.gradle file. It is only able to identify testng or junit, not both .What is the solution?Broch
In my case I used "SampleProject" (upper camel case) when generating a project with gradle init, and it used that as the "package name" The fix was to change the package name to lowercase.Deter
C
549

If you're using JUnit 5+, make sure you import the @Test annotation from the correct library:

import org.junit.jupiter.api.Test

not

import org.junit.Test

Celestyna answered 15/10, 2019 at 17:12 Comment(2)
But why? I have a Spring (not Spring Boot) project where org.junit.Test works just fine. But now I created a Boot project with Initializr and only org.junit.jupiter.api.Test works. Very confusing.Mercator
I spent 2 days to figure out, why my tests not working, checked build.gradle, all test dependencies but finally found the root cause by your anwser. Thank you for this. I think spring boot need to exclude jUnit 4 dependencies automatically. Let people get errors or issue instead of silently no test case running.Cataldo
D
537

Add to your build.gradle:

test {
    useJUnitPlatform()
}
Dumbbell answered 1/8, 2019 at 13:6 Comment(8)
As a reference from the documentation docs.gradle.org/current/userguide/…. The code above enables JUnit5 Platform to run the tests in your JVM project. Based on the dependencies or configuration the new Jupiter engine will execute those tests or if you need backward compatibility you can inject/configure Vintage (JUnit4) engine will do it.Dominickdominie
In Gradle's Kotlin DSL: https://mcmap.net/q/100766/-how-do-i-use-the-native-junit-5-support-in-gradle-with-the-kotlin-dslHipparch
Adding reference from another answer, for additional info. linkLiu
This seems not enough in my caseVanillic
Silly 🙃 that this isn't taken care of when doing a migration from Maven to Gradle with gradle init. Another consideration is the order of test in build.gradle, as in where it appears in the file.Sociolinguistics
Note that this produces a bug when it's used inside the android target of a Kotlin Multi-platform project.Gagman
This is happing with me when I am both using useJUnitPlatform() useTestNG() in build.gradle file. It is only able to identify testng or junit, not both .What is the solution?Broch
In my case I used "SampleProject" (upper camel case) when generating a project with gradle init, and it used that as the "package name" The fix was to change the package name to lowercase.Deter
J
241

I am using JUnit 4, and what worked for me is changing the IntelliJ settings for 'Gradle -> Run Tests Using' from 'Gradle (default)' to 'IntelliJ IDEA'.

enter image description here

Source of my fix: https://linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/

Jacqulynjactation answered 17/2, 2020 at 21:6 Comment(3)
@logbasex my understanding is that the default IntelliJ setting somehow becomes 'stale', and makes IntelliJ target the wrong testing framework. The original page is in korean so I don't understand it fully.Jacqulynjactation
That's "plan B", but Gradle won't rebuild that from scratch. Said otherwise, if you count on Gradle so someone else can use your project, don't do that, or make sure to push the IntelliJ project in your repo, but that defeats the purpose of using Gradle...Chipper
On Android Arctic Fox, there is no 'Run tests using' option.Torsion
C
71

Adding

test {
    useJUnitPlatform()
}

to the top level gave me the error "> Could not find method test() for arguments". Instead I had to add it to testOptions inside the android block

android {
    ...

    testOptions {
        unitTests.all {
            useJUnitPlatform()
        }
    }
}
Centuplicate answered 26/8, 2021 at 8:18 Comment(1)
Note that using useJUnitPlatform() produces a bug when it's used inside the android target of a Kotlin Multi-platform project.Gagman
W
52

Just in case this helps anybody, this happened to me on IntelliJ, and the case is, tests had been working before with Gradle but suddenly, next day some test stopped working giving this error.

What I did was:

  1. Go to Preferences -> Build, Execution, Deployment -> Gradle -> change "Run tests using" to "IntelliJ IDEA".
  2. Run your test.
  3. Go again to Preferences -> Build, Execution, Deployment -> Gradle -> change "Run tests using" to "Gradle (default)".
  4. Keep running your test, it is working now.

Doing this, I avoided having to change my local configuration or change Gradle files in the project.

enter image description here

Wingspread answered 14/11, 2021 at 16:52 Comment(2)
For me it didn't work until I reloaded gradle project after this change. Open gradle tool window and there is a refresh button on the toolbar.Saccharase
This happened to me when I forget to add 'org.junit.jupiter:junit-jupiter-engine' to testRuntime. ( test failed with : org.junit.platform.commons.PreconditionViolationException: Cannot create Launcher without at least one TestEngine; consider adding an engine implementation JAR to the classpath) Switching to IDEA engine covered this.Interbedded
P
51

To add to already great and easy solution provided by Przemek315, the same config if you use Kotlin DSL:

tasks.test {
    useJUnitPlatform()
}
Pelage answered 26/9, 2019 at 21:52 Comment(0)
H
29

I made the mistake of defining my test like this

class MyTest {
    @Test
    fun `test name`() = runBlocking {


        // something here that isn't Unit
    }
}

That resulted in runBlocking returning something, which meant that the method wasn't void and junit didn't recognize it as a test. That was pretty lame. I explicitly supply a type parameter now to run blocking. It won't stop the pain or get me my two hours back but it will make sure this doesn't happen again.

class MyTest {
    @Test
    fun `test name`() = runBlocking<Unit> { // Specify Unit


        // something here that isn't Unit
    }
}
Hostler answered 6/1, 2021 at 22:54 Comment(1)
I have spent 30 minutes on this thanks to you for saving me one and a half hour!Welldressed
B
20

Kotlin DSL: add to your build.gradle.kts

tasks.withType<Test> {
        useJUnitPlatform()
}

Gradle DSL: add to your build.gradle

test {
    useJUnitPlatform()
}
Barbel answered 4/10, 2020 at 11:13 Comment(3)
That's without type-safe accessor, otherwise see solution that had already been given by skryvets (https://mcmap.net/q/98565/-no-tests-found-for-given-includes-error-when-running-parameterized-unit-test-in-android-studio)Chipper
This solution for kotlin DSL works for me . And you need to put that in app/build.gradle.Reticle
Also added testImplementation(kotlin("test")) in dependencies, otherwise it's not enough.Alluring
L
19

Add in build.gradle,

test {
    useJUnitPlatform()
}

and use class org.junit.jupiter.api.Test for @Test, instead of org.junit.Test

Lancewood answered 10/8, 2021 at 16:0 Comment(1)
Thanks for suggesting this... this was my problem just now. I was using org.junit.Test.Mcgrew
A
17

Found a way to run the test in Android Studio. Apparently running it using Gradle Configuration will not execute any test. Instead I use JUnit Configuration. The simple way to do so is go to Select your Test Class to run and Right Click. Then choose Run. After that you'll see 2 run options. Select the bottom one (JUnit) as per the imageenter image description here

(note: If you can't find 2 Run Configuration to select, you'll need to remove your earlier used Configuration (Gradle Configuration) first. That could be done by Clicking on the "Select Run/Debug Configuration" icon in the Top Toolbar.

Acreinch answered 29/5, 2015 at 6:24 Comment(1)
This doesn't work.Volta
B
15

For anyone that is getting the error: Expression 'test' cannot be invoked as a function when adding the proposed solutions, try instead

tasks.withType<Test> {
    useJUnitPlatform()
}

Works with a KMM setup in 2021.

Bulbiferous answered 27/9, 2021 at 14:31 Comment(0)
C
14

If you are using intellij and want to use gradle you need to add this to the dependencies section of build.gradle file:

testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
Callihan answered 17/7, 2020 at 17:2 Comment(2)
Thank you. I was accidentally using junit-platform-engine instead of junit-jupiter-engine which apparently caused this issue.Honebein
Thank you! I was missing the testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.7.0") declarationEwen
T
11

I recently faced this issue and after deep-dive, I got to find out that I was using @Test annotation from junit library. However, my build.gradle file declared another test library.

Therefore, when I used @Test annotation from Test library present in build.gradle then it worked.

Timoshenko answered 22/9, 2021 at 5:6 Comment(0)
D
10

You could check if your method is private. Spent a lot of time fixing this dumb mistake ...

Disario answered 19/10, 2020 at 13:54 Comment(0)
G
6

For me, the cause of the error message

No tests found for given includes

was having inadvertently added a .java test file under my src/test/kotlin test directory. Upon moving the file to the correct directory, src/test/java, the test executed as expected again.

Gully answered 19/8, 2020 at 7:9 Comment(0)
C
5

Hard to believe but the issue in my case was the wrong name for the package that I created for my test cases. It shouldn't contain uppercase letters. For example, these cases are not correct: "PackageName" "Packagename" "packageName"

Combe answered 28/4, 2022 at 13:58 Comment(0)
A
5

I got a similar error in Intellij when using JUnit 4.

The fix was to remove from build.gradle.kts

    test {  useJUnitPlatform()  }

The result build.gradle.kts

plugins {
    kotlin("jvm") version "1.8.0"
    application
    id("org.jetbrains.kotlin.plugin.serialization") version "1.5.31"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.google.code.gson:gson:2.8.8")
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
    implementation ("org.apache.poi:poi-ooxml:5.1.0")
    testImplementation(kotlin("test"))
    testImplementation("junit:junit:4.13.1")
}



kotlin {
    jvmToolchain(11)
}

application {
    mainClass.set("MainKt")
}

And the tests work fine

import org.junit.Assert


class MainKtTest1 {
    @org.junit.Test
    fun firstTest() {
        Assert.assertEquals("2*2=4", 2 * 2, 4)
    }

}
Art answered 16/5, 2023 at 13:19 Comment(0)
F
3

In my case, the problem occurred when writing in Kotlin and using IDEA 2020.3. Despite proper entries in build.gradle.kts. It turned out that the problem was when generating test functions by IDEA IDE (Alt + Insert). It generates the following code:

@Test
   internal fun name () {
     TODO ("Not yet implemented")
   }

And the problem will be fixed after removing the "internal" modifier:

@Test
   fun name () {
     TODO ("Not yet implemented")
   }
Felicity answered 7/12, 2020 at 21:49 Comment(0)
L
3

For JUnit 5, we should use import org.junit.jupiter.api.Test and also should use @ExtendWith instead of @RunWith.

Lutes answered 14/5, 2021 at 9:0 Comment(0)
H
3

I added one string to the gradle build dependency list:

dependencies {
    implementation("org.junit.jupiter:junit-jupiter-params:5.8.0")
    testImplementation(kotlin("test"))
}

Rebuild gradle and everything works.

And remember to add the Test Files:

import org.junit.jupiter.params.ParameterizedTest
Heptastich answered 15/9, 2021 at 14:21 Comment(0)
F
2

Allow Run tests for all sub-modules using Intellij IDEA by command or UI.

subprojects {
    withType<Test> {
        useJUnitPlatform()
    }
}
Few answered 9/4, 2021 at 22:58 Comment(0)
H
2

I had to change the @Test library. At first I had imporetd org.junit.Test and I was getting this exception. Then I changed it to org.junit.jupiter.api.Test and it worked.

Hildredhildreth answered 3/8, 2023 at 10:29 Comment(0)
G
1

In my case I was getting this message due to a runtime error with Junit which wasn't at all visible from the output of the gradle test task execution. I've run into this for a couple reasons:

  1. Including the org.junit.platform:junit-platform-launcher dependency with a version that didn't match the junit version I was using
  2. Having an entry in META-INF/services for a Junit test listener which I had commented out

You can try re-running with --debug and search for FAILED or org.gradle.api.internal.tasks.testing.TestSuiteExecutionException. In my second case, the exception was:

2020-10-20T11:34:26.517-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.517-0700 [DEBUG] [TestEventLogger] Gradle Test Executor 1 STARTED
2020-10-20T11:34:26.661-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger] Gradle Test Executor 1 FAILED
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]     org.gradle.api.internal.tasks.testing.TestSuiteExecutionException: Could not complete execution for Gradle Test Executor 1.
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:63)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:33)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:94)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at com.sun.proxy.$Proxy2.stop(Unknown Source)
2020-10-20T11:34:26.662-0700 [DEBUG] [TestEventLogger]         at org.gradle.api.internal.tasks.testing.worker.TestWorker.stop(TestWorker.java:132)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.lang.reflect.Method.invoke(Method.java:498)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:36)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:182)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:164)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:413)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:48)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:56)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]         at java.lang.Thread.run(Thread.java:748)
2020-10-20T11:34:26.663-0700 [DEBUG] [TestEventLogger]
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]         Caused by:
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]         java.util.ServiceConfigurationError: org.junit.platform.launcher.TestExecutionListener: Provider com.example.myproject.MyCommentedOutClass not found
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader.fail(ServiceLoader.java:239)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader.access$300(ServiceLoader.java:185)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:372)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:404)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at java.lang.Iterable.forEach(Iterable.java:74)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:94)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:67)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.processAllTestClasses(JUnitPlatformTestClassProcessor.java:97)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor$CollectAllTestClassesExecutor.access$000(JUnitPlatformTestClassProcessor.java:79)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.junitplatform.JUnitPlatformTestClassProcessor.stop(JUnitPlatformTestClassProcessor.java:75)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.stop(SuiteTestClassProcessor.java:61)
2020-10-20T11:34:26.664-0700 [DEBUG] [TestEventLogger]             ... 25 more

Notice that these are DEBUG logs. I didn't see anything helpful with just --info

Gatefold answered 20/10, 2020 at 18:39 Comment(0)
C
1

I use @Test annotiation of org.junit.Test package, but I had the same problem. After adding testImplementation("org.assertj:assertj-core:3.10.0") on build.gradle, it worked.

Chorizo answered 1/11, 2020 at 17:10 Comment(0)
K
1

Steps:

  1. Add platform runner in build.gralde
    => testCompile group: 'org.junit.platform', name: 'junit-platform-runner', version: '1.7.0'
  2. Anotate test class with @RunWith => @RunWith(JunitPlatform.class)
Khudari answered 21/11, 2020 at 16:35 Comment(0)
C
1

For me it worked when I've added @EnableJUnit4MigrationSupport class annotation.

(Of course together with already mentioned gradle libs and settings)

Colossus answered 13/12, 2020 at 16:49 Comment(0)
D
1

My set up is Android Studio, Junit 4, test with robolectric.

The problem was I added this noverify setting to app level build.gradle in attempt to solve Java.lang.VerifyError: Bad return type from Android Studio run test with code coverage. (https://github.com/robolectric/robolectric/issues/3023)

enter image description here

Removing it fix the problem.

Donnadonnamarie answered 4/9, 2021 at 0:48 Comment(1)
I have multimodule project, just kindly forget to add robolectric.properties file inside test/resource folder because i only enabling it for sdk 19 & 29.Taunyataupe
S
1

Encountered this issue and I want to share my experience hope it may help other SO users that may have similar experiences.

I have tried useJUnitPlatform() on gradle and however they don't work for me.

At last I noticed that I did an auto convert from Java to Kotlin on the test class, which converted some of the @Test annotation to something like:

@get:Test

Apparently by removing/correcting these annotations, all tests worked and no more errors of no tests found for given includes ...

Just remove these "incorrect" annotations and no more changes.

Scourings answered 9/6, 2023 at 12:6 Comment(0)
B
1

In my case, it was solved by deleting .idea in the project folder.

Boozy answered 1/9, 2023 at 8:37 Comment(0)
T
1

In my case file and class name of the test didn't match, and that was the cause.

Eg. file name SomeTest.kt and class class SomeNewTest { ... }

Topknot answered 21/11, 2023 at 13:49 Comment(0)
T
1

I accidentally created an abstract class instead of regular class and added test there so it didn't run because no subclass was created from it.

was

abstract class MyTest {

   @Test
   fun test1() {
       // ...
   }
} 

should be

class MyTest {

   @Test
   fun test1() {
       // ...
   }
} 
Topknot answered 28/11, 2023 at 12:27 Comment(1)
This is a career-ending error! ha ha ha ha, glad you made a come back.Ligniform
M
0

I was also facing the same problem. In My case, I am using JUnit 5 with gradle 6.6. I am managing integration test-cases in a separate folder call integ. I have to define a new task in build.gradle file and after adding first line -> useJUnitPlatform() , My problem got solved

Murphree answered 28/9, 2020 at 0:44 Comment(0)
S
0

When I use junit5, it's working. But every time I execute gradle --clean, I get Class not found error. Then I add this to build.gradle to resolve my problem and I can still use junit4:

test {
}
Signorelli answered 18/11, 2020 at 6:48 Comment(0)
T
0

I needed to add robolectric dependency to resolve this issue.

Torsion answered 30/6, 2021 at 8:38 Comment(1)
It was my case too.Tuberculate
A
0

I was using a private method. Changing the access modifier to public helped me.

Aschim answered 13/8, 2021 at 11:47 Comment(2)
Can you give a more complete example of what you did?Rawboned
@akubi I would assume changing something like @Test private void test() to be @Test public void test(). However, by simply switching to using JUnit5, this removes the use of access modifiers on tests, @Test void test().Saw
L
0

In my case, I was running my test class in Intellij and saw No tests were found. I had marked my test methods as static - removing this fixed my issue.

Lush answered 2/10, 2021 at 17:26 Comment(0)
D
0

In my case, none of these solutions worked. The issue was a problematic dependency being imported — junit.vintage.

Adding exclude group: “org.junit.vintage”, module: “junit-vintage-engine” to my build.gradle fixed this.

Dubonnet answered 2/6, 2023 at 18:12 Comment(1)
Could you please specify the aexct line of code?Whyte
F
0

Same issue.

Caused by: org.gradle.api.GradleException: No tests found for given includes: [com.example........FibonacciTest]

Possibly that there is a class not found exception. You can check exactly the package path of the test cases, to see if there are any errors with the folder structure. It should be case-sensitive.

Fari answered 25/6, 2023 at 3:13 Comment(0)
L
0

In my case I encountered this error when trying out Jetbrains' Kotlin Test JUnit which is a Kotlin-specific testing library, an alternative to the Java's JUnit.

I encountered this error:

Test Events were not received Execution failed for task ':app:testDebugUnitTest'. No tests found for given includes: [com...TextExample](--tests filter)

To rectify the error I followed Kotlin Docs on how to set-up Jetbrains Kotlin Test JUnit on Intellij using Kotlin DSL.

I did the following on Android Studio:

Step 1 - Added this line to mydependencies: i.e. inside build.gradle.kts (app-level) file

dependencies { 
...

testImplementation(kotlin("test"))
...

}

Don't mix the above dependency with the other Kotilin-JUnit Libs to avoid ambiguity.

Step 2 (Optional) - Added the test task block i.e. inside build.gradle(.kts) (project-level) file: If for any reason you have to use this step then it has to be on the Android Project Level build.gradle file.

// Top-level build file where you can add configuration ...

    plugins {
        id("com.android.application") apply false
        ....
    }
    
    // add this block on project-level build.gradle.kts
    tasks.withType<Test> {

        useJUnitPlatform()

    }

There is also another way that worked for me by adding this dependency using the latest Kotlin Version (in Dec. 2023 it was 1.9.10)

testImplementation ("org.jetbrains.kotlin:kotlin-test-junit:1.9.10")

This will allow you to use the JetBrains Kotlin Test like this:

import kotlin.test.Test

class TextExample {

  @Test
    fun  exampleTest(){

    val result =  2 + 2
    assert(result == 4)
    }
}

I noted something very strange with the @Test annotation highlight color between the Java's and Jetbrains' Kotlin JUnit Libraries as depicted below:

enter image description here

Anyway, the color doesn't worry me as long as the tests run as intended.

PS If you come across a test error, it is advisable to first run the ExampleUnitTest that comes with the Android project and see if it will pass then fix it according to the error message.

Ligniform answered 31/12, 2023 at 16:30 Comment(0)
S
0

For me this worked

  tasks.withType(Test).configureEach {
    useJUnitPlatform()
  }
Surplice answered 17/4 at 17:12 Comment(0)
C
-1

Just adding... Maybe will help someone else...

In my case, this problem happened because of a case difference in the namespace of the unit test. One letter was upper case instead of lower case (camelCase instead of camelcase).

Claw answered 1/10, 2021 at 17:2 Comment(0)
C
-1

Writing @ParameterizedTest methods in Kotlin, I had to add the following dependency in my build.gradle file:

dependencies {
   testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

Apparently JUnit 5 separates out the unit testing API and the runtime engine, and you need to include both for it to pick up your unit tests.

Curiosa answered 4/10, 2021 at 16:0 Comment(0)
S
-2

It helps to add: test {useJUnitPlatform()} but you also need to have recent springBootVersion for me it worked with '2.4.5'

Sanfred answered 21/6, 2021 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.