简体   繁体   English

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

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

I am experiencing a strange behavior of Intellij IDEA 2016.3.我遇到了 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.当我得到java.lang.Exception: No tests found matching Method foo时,有一个带有方法foo的类和该方法的 JUnit 测试java.lang.Exception: No tests found matching Method foo运行测试时java.lang.Exception: No tests found matching Method foo的测试。 After I do mvn test it succeeds and then running the unit test right after executing mvn command it suddenly runs green.在我做mvn test它成功了,然后在执行 mvn 命令后立即运行单元测试它突然运行绿色。 Seems like IDEA does not compile automatically.似乎 IDEA 不会自动编译。 How can I fix this?我该如何解决这个问题?

PS No settings were altered after upgrading to v. 2016.3 PS 升级到 v. 2016.3 后没有更改任何设置

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

The same issue i got with Gradle (4.5+) + new Build Cache feature我在 Gradle (4.5+) + 新的构建缓存功能中遇到了同样的问题

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 ;)解决方案:清理.gradlebuildout目录,然后重试;)

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):好吧,在对每个单元测试的运行配置进行“播放”之后,我注意到每个运行配置Before Launch选项中都有一个Build目标预设(见下图): 在此处输入图片说明

After changing Build to Build Project the tests run fine.Build更改为Build Project ,测试运行良好。

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".如果您使用与“foo”相同的原始运行配置来运行“fooBar”,它仍会查找名为“foo”的测试,但它没有找到(因此是异常),因为它已重命名为“fooBar”。 The new Run Configuration would correctly look for "fooBar" test.新的运行配置将正确查找“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”名称。

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

In addition to the other answers here: the error can also happen when you forget @Test before your test method declaration.除了这里的其他答案:当您在测试方法声明之前忘记@Test时,也会发生错误。 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. IntelliJ (2018.1) 仍会向您显示用于测试执行的绿色“播放按钮”,但您的测试类中的公共方法将不是实际测试。

确保您的@test方法以及测试类都是public

如果没有在测试方法上方放置@Test注解,也会出现这种情况。

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.查看您的测试类是否正在扩展与 JUnit 3 相关的 TestCase 抽象类。 In order to fix this you have to start your method name with "test".为了解决这个问题,您必须以“test”开头您的方法名称。 For example public void testFoo() .例如public void testFoo()

If JUnit 3 is not the case, you're missing @Test annotation from your JUnit 4 test method.如果 JUnit 3 不是这种情况,那么您将缺少 JUnit 4 测试方法中的@Test注释。

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.注意:如果您从 TestCase 扩展并且测试方法用@Test注释并且您的方法名称以“test”开头,那么您可能将 JUnit 3 与 JUnit 4 混合在一起。不要这样做。 It will lead to other errors such as methods that annotated with @Ignore will not be ignored if those methods' names start with "test".它将导致其他错误,例如如果这些方法的名称以“test”开头,则不会忽略用@Ignore注释的方法。

Maybe you just give a wrong name for test method.也许你只是给测试方法取了一个错误的名字。

I met this problem because I used '—' instead of '_' which intelliJ cannot represent.我遇到了这个问题,因为我使用了 '—' 而不是 intelliJ 无法表示的 '_'。

Make sure you've correct runner mentioned above your class.确保你有上面提到的正确的跑步者。

I was getting this weird message when I was using runner CucumberWithSerenity.class .当我使用 runner CucumberWithSerenity.class时,我收到了这条奇怪的消息。 When I changed to SerenityRunner.class it got fixed.当我更改为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 ) 可以显示一些比这更好的错误信息

I had to add test before the test Method Name.我必须在测试方法名称之前添加测试。

methodtest() does not work but testMethod() worked methodtest()不起作用但testMethod()起作用

Make sure @org.junit.Test is added on top of your method.确保@org.junit.Test添加到您的方法之上。 I forgot them and that fixed it for me!我忘了他们,这为我修好了!

You may check that Run/Debug Configurations in the top of the IntelliJ screen.您可以检查 IntelliJ 屏幕顶部的运行/调试配置。 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.您也可以在右侧的 maven 选项卡上重新加载项目。

电脑 InteliJ 屏幕

unfortunately some versions of Junit you must start the Test method name with test.不幸的是,某些版本的 Junit 必须以 test 开头的测试方法名称。 Otherwise, it won't work.否则,它不会工作。

test MyTestingCase()测试MyTestingCase()

@Test
public void testMyTestingCase() {
} 

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 Build > Clean Project解决了这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.Exception:找不到匹配的测试 - java.lang.Exception: No tests found matching Intellij IDEA看不到测试(java.lang.Exception:类中没有测试) - Intellij IDEA Doesn't See Tests (java.lang.Exception: No tests found in class) 在RunWith批注中使用SerenityParameterizedRunner进行的junit测试:未找到测试(java.lang.Exception:未找到与Method匹配的测试) - junit Test with SerenityParameterizedRunner in RunWith annotation: does not find tests (java.lang.Exception: No tests found matching Method) java.lang.Exception:找不到TestComplete / TestExecute - java.lang.Exception: TestComplete/TestExecute not found Vaadin主题:java.lang.Exception:Mixin定义:找不到valo - Vaadin Themes: java.lang.Exception: Mixin Definition: valo not found 一个方法可以抛出java.lang.Exception而不声明它吗? - Can a method throw java.lang.Exception without declaring it? 在android活动中实现方法时的java.lang.Exception - java.lang.Exception when implementing method in android activity 如何使用JAXB Marshaller封送java.lang.Exception? - How to marshall java.lang.Exception using JAXB Marshaller? 代码中的“未处理异常:Java.lang.exception” - "Unhandled exception: Java.lang.exception" in code 未报告的异常java.lang.exception - Unreported exception java.lang.exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM