53

I am experiencing a strange behavior of Intellij IDEA 2016.3. Having a class with method foo and a JUnit test for the method when I get java.lang.Exception: No tests found matching Method foo when running the test. After I do mvn test it succeeds and then running the unit test right after executing mvn command it suddenly runs green. Seems like IDEA does not compile automatically. How can I fix this?

P.S. No settings were altered after upgrading to v. 2016.3

Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
  • 2
    Look at the run configuration for the test. If you see a Make, then IntelliJ will compile again when you change the test. Since this is a Maven project, I'll assume your test class is in src/test/java. – duffymo Nov 29 '16 at 10:36
  • @duffymo You're right, I had a corrupt run config for the test. After deleting the corrupt one and rerunning the test it worked fine. You can put your comment as an answer so I can accept it. – Arthur Eirich Nov 29 '16 at 10:43
  • No worries Arthur, glad to help. – duffymo Nov 29 '16 at 10:45
  • @duffymo Today the problem occured again, even having no run configuration for test class and just running the test caused the problem – Arthur Eirich Nov 30 '16 at 14:39
  • @duffymo I tried changing the `Before launch` option to `Build project` instead of `Build` and suddenly everything began to work properly. – Arthur Eirich Nov 30 '16 at 14:41
  • Finally a configuration problem for me. I restarted the IDE and reviewed my project setting... And now it works like a charm ! – Karbos 538 Jul 19 '17 at 07:32
  • trick with "build project" doesn't work for gradle. solution described in answer – Eugene Lebedev Mar 04 '18 at 10:48
  • for me, @RunWith(Enclosed.class) was the issue. The test were not in enclosed classes. Putting them in enclosed classes solved the issue. – Gaurav Mar 20 '19 at 06:26

16 Answers16

43

If you're using a theory testing framework like Junit's or Robolectric's, make sure to run the class containing the test you want, instead the test itself. Since these frameworks use the test methods as instance methods instead of static methods, any testing framework looking for a normal public static test won't find anything.

user70585
  • 1,397
  • 1
  • 14
  • 19
  • 1
    I'm used to running only test method, not the whole class. But trying your solution lead me to the actual error - that the method I tried to run was not public (and I had this problem on Eclipse). – Line Feb 11 '19 at 15:20
27

The same issue i got with Gradle (4.5+) + new Build Cache feature

Sometimes it's unable to find new test methods and throws exception (like you mentioned in topic)

Solution: clean .gradle, build and out directories and try again ;)

Eugene Lebedev
  • 1,400
  • 1
  • 18
  • 30
  • 2
    that's my correct answer, I've been using IntelliJ so it's unrelated to eclipse. deleting everything and rebuild works! solved my 1h wasting issue... thanks! – TecHunter Jul 11 '18 at 12:35
13

Well, after "playing" a bit with run configurations of each unit test I noticed that each Run Config has a Build goal preset in the Before Launch option (See pic below): enter image description here

After changing Build to Build Project the tests run fine.

Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63
  • I am also missing the `Make` Option from older IDEA versions where tests were all running. Is this a kind of bug or should I change smth. to get the tests running. I don't want to alter the presets for each run config of each test. – Arthur Eirich Nov 30 '16 at 15:06
9

If you originally run a test named "foo", and then rename it to "fooBar", you must subsequently run "fooBar" with a new Run Configuration.

If you use the same original Run Configuration for "foo" to run "fooBar", it still looks for a test named "foo" which it does not find (thus the Exception) because it was renamed to "fooBar". The new Run Configuration would correctly look for "fooBar" test.

I made this mistake unknowingly because I renamed a test, but then just clicked the green run button in IntelliJ: Doing that runs the last Run Configuration, which in this scenario has the old "foo" name.

cellepo
  • 4,001
  • 2
  • 38
  • 57
8

Deleting Intellij's out directory fixed this issue for me.

user3026571
  • 171
  • 2
  • 7
8

In addition to the other answers here: the error can also happen when you forget @Test before your test method declaration. IntelliJ (2018.1) will still show you the green "Play-Button" for test execution, but that public method in your Test-Class will not be an actual test.

Phil
  • 7,065
  • 8
  • 49
  • 91
6

Make sure that your @test methods as well as the test class are public.

Samuli Pahaoja
  • 2,660
  • 3
  • 24
  • 32
Kyle Anderson
  • 61
  • 1
  • 2
3

Since you got your answer and for others searching for solution,

Look if your test class is extending TestCase abstract class which is a JUnit 3 related. In order to fix this you have to start your method name with "test". For example public void testFoo().

If JUnit 3 is not the case, you're missing @Test annotation from your JUnit 4 test method.

Note: If you're extending from TestCase and test methods are annotated with @Test and also your methods' names start with "test", then probably you're mixing JUnit 3 with JUnit 4. Don't do that. It will lead to other errors such as methods that annotated with @Ignore will not be ignored if those methods' names start with "test".

skaveesh
  • 369
  • 6
  • 9
2

This situation can also occur if you do not place @Test annotation above the test method.

RobC
  • 22,977
  • 20
  • 73
  • 80
Kuba Skiba
  • 21
  • 1
1

Maybe you just give a wrong name for test method.

I met this problem because I used '—' instead of '_' which intelliJ cannot represent.

Ming Arron
  • 21
  • 3
1

I had to add test before the test Method Name.

methodtest() does not work but testMethod() worked

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Akh
  • 11
  • 3
1

Make sure @org.junit.Test is added on top of your method. I forgot them and that fixed it for me!

benmaq
  • 148
  • 1
  • 1
  • 7
0

Make sure you've correct runner mentioned above your class.

I was getting this weird message when I was using runner CucumberWithSerenity.class. When I changed to SerenityRunner.class it got fixed.

@RunWith(SerenityRunner.class)
//@RunWith(CucumberWithSerenity.class)
public class WordPressAppTest {

I'm using Serenity framework for web automation and use below runner class

import net.serenitybdd.cucumber.CucumberWithSerenity;
import net.serenitybdd.junit.runners.SerenityRunner;
import org.junit.runner.RunWith;

I feel IDEA ( 2017.2.6 ) can show some better error message than this

vikramvi
  • 3,312
  • 10
  • 45
  • 68
0

You may check that Run/Debug Configurations in the top of the IntelliJ screen. Delete all of then with the "minus button" and hit "run" green button again to run the test.

You may reload the project on the maven tab on the right as well.

computer InteliJ screen

0

In my spring mvc project. I solved this problem by adding

@RunWith(SpringJUnit4ClassRunner.class)

to my test class.

Waqar Detho
  • 1,502
  • 18
  • 17
-1

In my case, I copied a test from another class and modified it, but while running the test it was still pointing to the previous one.

Build > Clean Project solved the problem

dianakarenms
  • 2,609
  • 1
  • 22
  • 22