Reputation: 945
I cannot run tests via Gradle in IntelliJ IDEA because of "No tests found for given includes" error.
How can I fix it?
GradleTests
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GradleTests {
@Test
public void initTest() {
assertTrue(true);
}
}
build.gradle
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
//testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.0'
}
test {
useJUnitPlatform()
}
Error:
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [GradleTests.initTest](filter.includeTestsMatching)
Some notes:
src/test/java
useJUnitPlatform()
result is the sameUpvotes: 52
Views: 72465
Reputation: 1648
In my case this was my error:
Execution failed for task ':test'.
> No tests found for given includes: [org.bz.app.mspeople.controllers.UserControllerIntegrationSpec](--tests filter)
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.6/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 11s
5 actionable tasks: 2 executed, 3 up-to-date
I had:
@DisplayName("Integration tests for UserController endpoints")
@Tag("integration")
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserControllerIntegrationSpec extends Specification {
@Autowired
private WebApplicationContext context
@Autowired
private UserService userService
@Autowired
private TokenService tokenService
@Autowired
private ObjectMapper objectMapper
private MockMvc mockMvc
void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(springSecurity())
.build()
}
@WithMockUser(authorities = ["READ_ALL"])
def "list_using_Authority"() {
def param = (MockHttpServletRequestBuilder) MockMvcRequestBuilders
.get("/api/users")
def resultActions = mockMvc.perform(param)
.andDo(print())
resultActions.andExpect(status().isOk())
}
}
I solved adding: given:
, when:
, then:
@WithMockUser(authorities = ["READ_ALL"])
def "list_using_Authority"() {
given:
def param = (MockHttpServletRequestBuilder) MockMvcRequestBuilders
.get("/api/users")
when:
def resultActions = mockMvc.perform(param)
.andDo(print())
then:
resultActions.andExpect(status().isOk())
}
And the answer was:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /api/users
Parameters = {}
Headers = []
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = org.bz.app.mspeople.controllers.UserController
Method = org.bz.app.mspeople.controllers.UserController#list()
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 200
Error message = null
Headers = [Vary:"Origin", "Access-Control-Request-Method", "Access-Control-Request-Headers", Content-Type:"application/json", X-Content-Type-Options:"nosniff", X-XSS-Protection:"0", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
Content type = application/json
Body = []
Forwarded URL = null
Redirected URL = null
Cookies = []
Upvotes: 0
Reputation: 3
I accidentally put a .java test file into a kotlin folder and get this same error. Once I moved the file from src/test/kotlin to src/test/java folder the error went away. Doh!
Upvotes: 0
Reputation: 1514
My problem was the filename that contained the test class did not match the class name. So I had AppRepositoryTest.kt
with FetchDataUseCaseTest
test class inside. Apparently Junit 4 does not like that, so once I made both names the same it started working.
Upvotes: 0
Reputation: 1190
After going through all configurations and finding nothing, a simple gradle clean fixed this very persistent issue for me, and i could keep using "run tests with gradle".
./gradlew :domain:test --tests "..."
> Task :domain:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':domain:test'.
> No tests found for given includes: ...
./gradlew clean
./gradlew :domain:test --tests "..."
BUILD SUCCESSFUL in 8s
Upvotes: 0
Reputation: 622
In my case, I had the wrong import.
I needed import org.junit.jupiter.api.Test;
instead of import org.junit.Test;
Upvotes: 13
Reputation: 674
I've got an Error: No tests found for given includes: ... (filter.includeTestsMatching).
After I checked JUnit dependency I discovered junit-4.13.1 version.
Fixed with replacing this dependency to org.junit.jupiter.api-5.7.0.
Upvotes: 1
Reputation: 189
/app/build.gradle
android {
testOptions {
unitTests.all {
useJUnitPlatform()
}
}
}
Upvotes: 15
Reputation: 1799
Gradle has no idea where to look for tests. Add this to your app build.gradle file root(Not inside android or dependencies closure):
tasks.withType(Test) {
useJUnitPlatform()
testLogging { // This is for logging and can be removed.
events("passed", "skipped", "failed")
}
}
Upvotes: 20
Reputation: 3286
Another cause of this behaviour when using IntelliJ to edit a Kotlin project is the folders used for the tests, Java classes need to be in a subfolder of java and Kotlin classes in a subfolder of kotlin.
Here's an example of a mini-project I created which demonstrates the folder structure https://github.com/znsio/AutomatedTestingWithKotlin/tree/main/src/test
I found the explanation which is quoted below together with the link to the source:
"My problem was in single path to kotlin and java tests. So kotlin tests are in root/src/test/kotlin/package
and they are running fine with gradle :cleanTest :test
and java tests must be in root/src/test/java/package
. Otherwise neither compileTestKotlin
nor compileTestJava
will not find java tests to compile."
https://discuss.kotlinlang.org/t/not-able-to-run-unit-tests-on-kotlin-multiplatform-project/15779/7
Upvotes: 2
Reputation: 1432
I had this error with a similar setup, but couldn't solve it with the previous answers. Resolved it by doing this.
All credit to: https://linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/.
Upvotes: 91
Reputation: 11
Gradle is case sensitive in choosing its selector. See here You may need to change "GradleTests" to "gradleTests"
Upvotes: 1
Reputation: 945
Thanks to Ben Watson I've found solution. Since JUnit 5.4.0 there is aggregate artifact with both api and engine dependencies. So just adding one dependency to build.gradle resolved this issue.
testCompile ('org.junit.jupiter:junit-jupiter:5.6.0')
Upvotes: 16