No tasks available when executing JUnit runner class

When I had this problem ("No tasks available" message when trying to run a test), what worked for me was to simply re-import the project from the Gradle view.

Right-click on project in Gradle view and select Reimport Gradle Project.

"No tasks available" – I got this message when trying to run Spock test.

The reason was I did not have gradle plugins configured properly:

plugins {
    id 'java-library'
    id 'groovy' // this one is also necessary

Make sure you have 'groovy' plugin enabled, then re-import your project.

Ok, none of the proposed solutions worked, but I finally figured it out.

Went to Settings > Build, Execution, Deployment > Build Tools > Gradle and changed Run tests using: from Gradle (Default) to IntelliJ IDEA.

enter image description here

Note: found the solution here.

I was facing the same issue. When using gradle make sure your project structure is correct. Your tests should be in src>test>java

This resolved the issue for me.

Java Test Structure