Kannan Ekanath
Kannan Ekanath

Reputation: 17631

In Intellij, right click on a test does not present a "Run" option

In IntelliJ when I right click on a test I dont see a "Run ClassX or MethodY" anymore. In fact there is no "run" window and when I right click I cannot run any class.

It was all working fine about 3 hours ago so I am not quite sure what has changed.

IntelliJ 10.5.1 (Licensed and NOT community edition)
Java 1.6.0_24

This happens for all projects.

Update 1

I installed IDEA 11 and imported settings from 10 and then saw that it was not a free upgrade close IDEA 11 and started using IDEA 10. I am fairly certain things stopped working from that point but not sure. Is that a problem? Can I somehow delete IntelliJ configuration directory somehow and restart?

Adding a screenshot when I don't get Run option on right click:

enter image description here

Upvotes: 68

Views: 141347

Answers (27)

Joanna Mamczynska
Joanna Mamczynska

Reputation: 2208

I had this problem with a Kotlin project. Following action solved it:

  • right click on test folder -> select Mark Directory as -> choose Test Sources Root

mark as Test sources root

Upvotes: 7

Josiah Yoder
Josiah Yoder

Reputation: 3776

I had the same problem. To fix it, I had to ensure that my class had a proper main method:

public static void main(String[] args) {
}

Every keyword in the above statement is critical. If you omit one, IntelliJ wont' recognize it. Easy-to-forget keywords are static, void, and the String[] args argument.

I had forgotten the arguments in mine ;-)

Also make sure your source code is inside a src folder which is marked as such by IntelliJ.

Upvotes: 31

Line
Line

Reputation: 1651

If the project is already added as a Maven one, unlink it.

right click on the project -> Maven -> Unlink Maven Projects

Then link it again:

right click on pom.xml -> + Add as Maven Project

Upvotes: 3

user16037629
user16037629

Reputation:

Make sure the method is not static! JUnit only allows @Test before instance methods but Intellij doesn't complain even if you use @Test above a static method.

Upvotes: 0

Ajay
Ajay

Reputation: 207

Search for pom.xml file --> Right Click on pom.xml --> Click on Add as Maven Project.

You should get a pop-up, saying downloading Mavin Plugin.

enter image description here

Wait till it gets completed. It takes some time depending on the number of content in pom.xml.

In the below screenshot, "BackendApp.java" is Driver Java file that contains

public static void main(String[] args){}

Right Click on such driver java file and you see Run option in green color.

enter image description here

Upvotes: 1

gunslingor
gunslingor

Reputation: 1486

Seems there are version conflicts between plugins and IDEA itself that commonly break this functionality. Many of the solution here indicate people manually trying to identify the plug in, I found that to be impossible. So here is a generic way, without having to uninstall IDEA as one solution proposed:

File > Settings > Plugins > gear icon > disable all downloaded plugins

This fixed it for me.

Upvotes: 0

OfirD
OfirD

Reputation: 10510

Since this is a top Google result when trying to figure out how to run a Scala project, and since for some reason IntelliJ (I'm on 2020.1.2) doesn't automatically create a run configuration for a Scala project (in contrary to a Java project), it's worth laying out the basics for future readers:

  1. Click on "Edit Configuration" on the top right of the screen.
  2. Inside the opened window, click the + button its top left.
  3. Choose "Application".

Now, let's go through the required fields:

  1. in the "Name" field, enter a name for this configuration, e.g. "run".
  2. in the "Main Class" field, enter your main class name.
    Alternatively, use the ... button to the left: inside the window that opens, click the "Project" tab, navigate to your main class, pick it and click "ok".
    Note: If your main class doesn't have a main method, IntelliJ would show an alert. see listing 9 below.
  3. Make sure the value in "Working Directory" field is correct (it should be the project folder).
  4. in "Use classpath of module", select your module (see project vs module in IntelliJ)
  5. That's it for configuring. Click "Apply", and close the window. Here's an example final result:

enter image description here

  • Note that my Main class is part of a package named Demo, but having a package is not necessary.
  1. Inside the main class, make sure there's a main method.
    See @Josiah Yoder answer for java, and for Scala it's:

    def main(args: Array[String]): Unit = {
    }
    
  2. That's it. You should now have the green run and debug buttons enabled.

Upvotes: 0

boucekv
boucekv

Reputation: 1230

I had the same problem. I solve it by File -> Invalidate Caches / Restart right click on the top pom.xml -> Maven -> reimport enter image description here

Upvotes: 4

JFRANCK
JFRANCK

Reputation: 364

Unfortunately, none of these worked for me. I had to

  1. 'File' -> "Invalidate Caches / Restart"
  2. Click on pom.xml file, and then 'maven' -> "Generate Sources" maven, generate sources

Upvotes: 1

Guchelkaben
Guchelkaben

Reputation: 1285

If your project is a maven project then you can just right-click on the pom.xml file and select "add as Maven project".

This approach worked for me. (green plus third from the bottom)

enter image description here

Upvotes: 57

T04435
T04435

Reputation: 14032

In my case, my project is using Bazel.

The solution was to sync Bazel from Bazel plugin.

Upvotes: 0

Chandrahasan
Chandrahasan

Reputation: 2079

I could resolve the issue by disabling the Gradle plugin from the Plugin menu and restart.

Upvotes: 3

Guy Sadoun
Guy Sadoun

Reputation: 535

in my case, I did not have an output folder. file -> project structure -> under 'project' tab there's 'Project compiler output' -> define your folder.

Upvotes: 1

Tanino
Tanino

Reputation: 184

I had the same problem on my Intellij 2019.3 after that I updated from 2019.2.4. I thought that the problem came from the updated first, but the rollback didn't fix, so I tried de solution above e the problem was fixed. After configuring all my projects from scratch the problem get back, so I started to check everything I did. I discovered that an old project from Eclipse that uses files .launch to run and need some plugins to be able to execute on Intellij was causing the problem, after disable the following plugins the test option return.

plugins

Upvotes: 0

Etch
Etch

Reputation: 311

In my case i right clicked the src folder and went to -> mark directory as -> sources root.

Upvotes: 10

gouri panda
gouri panda

Reputation: 322

if you can see the play button in left side of the main function then click right click and press run.

Upvotes: 1

Thomm
Thomm

Reputation: 516

For me this happened after updating Idea, and then updating all Plugins. Apparently Idea had not restarted yet. Going to File -> Settings -> Plugins and clicking 'Restart Idea' solved the problem

Upvotes: 1

Tom
Tom

Reputation: 4093

If you're using JUnit 5 (Jupiter), this happens when you use the old @Test annotation from JUnit 4. Just replace

import org.junit.Test;

with

import org.junit.jupiter.api.Test;

and IntelliJ should show the "Run" button again.

Upvotes: 20

Pleymor
Pleymor

Reputation: 2921

Disabling gradle plugin solved the issue for me (community edition 2018.2)

Upvotes: 7

Dan
Dan

Reputation: 9801

I've just had the same problem and solved it in the following way.

Go to your $USER/$INTELLIJ directory e.g. $USER/.IdeaC2018.3 then find config/plugins. Rename the plugins directory and restart IntelliJ.

My guess is that the problem was caused when I upgraded IntelliJ and incompatibilities with the cucumber plugin.

Upvotes: 1

Naresh Kumar
Naresh Kumar

Reputation: 1

I had the same problem and I tried all the above solutions nothing worked for me but after I install TestNG plugin it's started working since there are some TestNG annotations used in my unit tests

Upvotes: 0

phil_20686
phil_20686

Reputation: 4080

So I had this problem on pycharm and the problem was that there was already a run configuration (in the dropdown next to the play button) that had the name of the file. When i deleted that run configuration it would create a new one that was correct.

Upvotes: 0

GreenGiant
GreenGiant

Reputation: 5266

My problem was that my test class wasn't public. I needed:

public class MyTest {

    @Test
    public void testMethod() {

instead of:

class MyTest {

    @Test
    void testMethod() {

Upvotes: 65

niranjan
niranjan

Reputation: 159

As i had the same issue , i could clearly See that @Test is not providing any hint when i press control key and hover , and the same was confirmed as External Libraries was not having Gradle Dependencies added so i had to update the project as gradle project suggested by a pop up when you start Intellij.

Upvotes: 2

user5779136
user5779136

Reputation:

In my case, the cause was disabled JUnit plugin. (File — Settings — Plugins — JUnit, check, OK)

Upvotes: 26

Stephen Anderson
Stephen Anderson

Reputation: 390

There should be no need to delete any configuration files.

I found that I used to have the Run option in the context menu to select either run tests or run Scala tests, etc. After I had selected an option for the first time, my options were no longer there.

I was able to resolve this issue and select the type of tests I wanted for that folder by creating a Run/Debug configuration following the instructions found in the documentation here...

https://www.jetbrains.com/help/idea/2016.1/creating-run-debug-configuration-for-tests.html?origin=old_help

Upvotes: 2

Kannan Ekanath
Kannan Ekanath

Reputation: 17631

Ok after tremendous amount of eyeballing I located a {HOME}/.java directory which seemed to contain some Jetbrains related preferences. I deleted that directory plus {HOME}/.IntelliJ* directories. Then deleted all my intellij installations and downloaded it again from scratch and it now works fine..

Sigh....

Upvotes: 9

Related Questions