java.lang.Exception: No tests found matching Metho

2019-03-24 07:09发布

问题:

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

回答1:

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 ;)



回答2:

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.



回答3:

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):

After changing Build to Build Project the tests run fine.



回答4:

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.



回答5:

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



回答6:

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.



回答7:

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



回答8:

Maybe you just give a wrong name for test method.

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