java.lang.Exception: 没有使用 Intellij IDEA 找到匹配方法的测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40863593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:57:39  来源:igfitidea点击:

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

javamavenintellij-ideajunitcompilation

提问by Arthur Eirich

I am experiencing a strange behavior of Intellij IDEA 2016.3. Having a class with method fooand a JUnit test for the method when I get java.lang.Exception: No tests found matching Method foowhen running the test. After I do mvn testit 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?

我遇到了 Intellij IDEA 2016.3 的奇怪行为。foo当我java.lang.Exception: No tests found matching Method foo运行测试时,有一个带有方法的类和一个用于该方法的 JUnit测试。在我执行mvn test成功并在执行 mvn 命令后立即运行单元测试后,它突然运行为绿色。似乎 IDEA 不会自动编译。我怎样才能解决这个问题?

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

PS 升级到 v. 2016.3 后没有更改任何设置

采纳答案by Arthur Eirich

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

好吧,在对每个单元测试的运行配置“玩”了一下之后,我注意到每个运行配置BuildBefore Launch选项中都有一个目标预设(见下图):在此处输入图片说明

After changing Buildto Build Projectthe tests run fine.

更改BuildBuild Project测试后运行正常。

回答by cellepo

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

如果您最初运行名为“foo”的测试,然后将其重命名为“fooBar”,则必须随后使用新的 Run Configuration 运行“fooBar”

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.

如果您使用与“foo”相同的原始运行配置来运行“fooBar”,它仍会查找名为“foo”的测试,但它没有找到(因此是异常),因为它已重命名为“fooBar”。新的运行配置将正确查找“fooBar”测试。

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.

我在不知不觉中犯了这个错误,因为我重命名了一个测试,然后只是点击了 IntelliJ 中的绿色运行按钮:这样做会运行最后一个运行配置,在这种情况下它具有旧的“foo”名称。

回答by user70585

If you're using a theory testing framework like Junit'sor 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 statictest won't find anything.

如果您使用的是诸如JunitRobolectric 之类的理论测试框架,请确保运行包含所需测试的类,而不是测试本身。由于这些框架使用测试方法作为实例方法而不是静态方法,任何寻找正常public static测试的测试框架都找不到任何东西。

回答by vikramvi

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.classit got fixed.

当我使用 runner 时,我收到了这条奇怪的消息CucumberWithSerenity.class。当我改变SerenityRunner.class它时,它得到了修复。

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

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

我正在使用 Serenity 框架进行 Web 自动化并在 runner 类下面使用

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

我觉得 IDEA ( 2017.2.6 ) 可以显示一些比这更好的错误信息

回答by Evgeny Lebedev

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

我在 Gradle (4.5+) + 新的构建缓存功能中遇到了同样的问题

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

有时它无法找到新的测试方法并抛出异常(就像您在主题中提到的那样)

Solution:clean .gradle, buildand outdirectories and try again ;)

解决方法:清洁.gradlebuildout目录,然后再试一次;)

回答by user3026571

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

删除 Intellij 的 out 目录为我解决了这个问题。

回答by Phil

In addition to the other answers here: the error can also happen when you forget @Testbefore 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.

除了这里的其他答案:当您@Test在测试方法声明之前忘记时,也会发生错误。IntelliJ (2018.1) 仍会向您显示用于测试执行的绿色“播放按钮”,但您的测试类中的公共方法将不是实际测试。

回答by Ming Arron

Maybe you just give a wrong name for test method.

也许你只是给测试方法取了一个错误的名字。

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

我遇到了这个问题,因为我使用了 '—' 而不是 intelliJ 无法表示的 '_'。

回答by Kyle Anderson

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

确保您的@test方法和测试类都是public.

回答by Kuba Skiba

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

如果您没有@Test在测试方法上方放置注释,也会发生这种情况。