java.lang.Exception: No tests found matching Method using Intellij IDEA
Asked Answered
A

17

54

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

Annunciate answered 29/11, 2016 at 10:33 Comment(8)
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.Extrinsic
@Extrinsic 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.Annunciate
No worries Arthur, glad to help.Extrinsic
@Extrinsic Today the problem occured again, even having no run configuration for test class and just running the test caused the problemAnnunciate
@Extrinsic I tried changing the Before launch option to Build project instead of Build and suddenly everything began to work properly.Annunciate
Finally a configuration problem for me. I restarted the IDE and reviewed my project setting... And now it works like a charm !Christcrossrow
trick with "build project" doesn't work for gradle. solution described in answerJodiejodo
for me, @RunWith(Enclosed.class) was the issue. The test were not in enclosed classes. Putting them in enclosed classes solved the issue.Gothard
A
14

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.

Annunciate answered 30/11, 2016 at 15:6 Comment(1)
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.Annunciate
C
44

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.

Clamshell answered 30/6, 2017 at 19:13 Comment(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).Derive
J
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 ;)

Jodiejodo answered 4/3, 2018 at 10:47 Comment(1)
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!Twayblade
A
14

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.

Annunciate answered 30/11, 2016 at 15:6 Comment(1)
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.Annunciate
G
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.

Glandular answered 17/5, 2017 at 20:3 Comment(1)
This happens in particular when the first time you ran the test, you selected the test method, not the whole class.Bullfighter
S
8

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

Sosna answered 9/10, 2018 at 22:43 Comment(3)
Fixed for a minute. It still doesn't know when to recompile my project.Rochellrochella
I just deleted my whole project and then clone it again. It's now working...No idea why :/Corella
Or delete the build, target, etc... directories and then build again.Sackcloth
H
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.

Husch answered 21/10, 2018 at 13:36 Comment(0)
B
6

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

Buttress answered 5/4, 2019 at 14:16 Comment(0)
M
3

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

Maleficent answered 2/9, 2019 at 9:4 Comment(0)
N
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".

Niggardly answered 22/5, 2020 at 12:27 Comment(0)
C
1

Maybe you just give a wrong name for test method.

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

Cai answered 14/2, 2019 at 9:21 Comment(1)
STS is best tool for Spring DevelopmentInterlaken
T
1

I had to add test before the test Method Name.

methodtest() does not work but testMethod() worked

Tongs answered 14/2, 2021 at 1:33 Comment(1)
works for me! thanks~Supernormal
D
1

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

Dragoman answered 6/4, 2021 at 13:37 Comment(0)
C
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

Catkin answered 17/11, 2017 at 6:27 Comment(0)
A
0

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

Anonym answered 18/6, 2020 at 18:17 Comment(0)
M
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

Maximalist answered 9/4, 2021 at 20:6 Comment(0)
M
0

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

@RunWith(SpringJUnit4ClassRunner.class)

to my test class.

Metsky answered 30/9, 2021 at 12:7 Comment(0)
K
0

It works for me to add the class name into @PrepareForTest list of the specific unit test, for example, I need to mock class A's function, I have to code:

@Test
@PrepareForTest({A.class, B.class})
public void testExample() throws Exception {
    Mockito.when(A.functionName(param1, param2)).thenReturn(true);
    ...
}
Kocher answered 3/5, 2024 at 23:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.