小编gla*_*aed的帖子

JUnit4和JUnit5测试未在IntelliJ中运行

我试图在IntelliJ IDEA 2017.1.5中的同一个项目中使用JUnit4和JUnit5测试.到目前为止,所有测试都基于JUnit4.我加了jupiter,platform并且vintage依赖于我的pom.xml(包括junit-platform-surefire-providerjunit-vintage-engine为保命插件).现在,我的JUnit4的示例测试和JUnit 5的测试都没有执行.

相反,我收到以下错误:

Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.commons.util.ReflectionUtils.getDefaultClassLoader()Ljava/lang/ClassLoader;
    at org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry.loadTestEngines(ServiceLoaderTestEngineRegistry.java:30)
    at org.junit.platform.launcher.core.LauncherFactory.create(LauncherFactory.java:53)
    at com.intellij.junit5.JUnit5IdeaTestRunner.createListeners(JUnit5IdeaTestRunner.java:39)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code 1
Empty test suite.
Run Code Online (Sandbox Code Playgroud)

我试图尽可能地遵循JUnit 5用户指南中的建议,但我可能错过了一些东西.如何让两个测试都正常运行?(当然还有我现有的所有测试)

JUnit 4测试类

package com.glaed.util;

import org.junit.Test;

public class JUnit4Test {

  @Test
  public void helloJUnit4Test() {
    System.out.println("Hello JUnit4!");
  }

}
Run Code Online (Sandbox Code Playgroud)

JUnit 5测试类

package com.glaed.util;

import org.junit.jupiter.api.Test;

class JUnit5Test {

  @Test
  void helloJU5test() …
Run Code Online (Sandbox Code Playgroud)

junit intellij-idea junit4 maven junit5

12
推荐指数
1
解决办法
1万
查看次数

为什么JUnit 5测试不从抽象类继承@Test注释?

我刚刚意识到(在将遗留代码从JUnit 4迁移到JUnit 5时)我们的一些测试方法没有执行,因为它们没有@Test注释.他们没有它,因为它们覆盖了抽象超类(注释存在的地方)的方法.

我可以通过添加@Test到每个方法轻松解决这个问题.但我想知道这是否是预期的行为.它从JUnit 4更改为5但我在官方JUnit5用户指南或其他任何地方都找不到任何相关内容.

根据这个问题,注释通常不会被继承.但似乎在新的JUnit版本中故意改变了这一点.(或者我错过了什么?)

抽象测试类

import org.junit.jupiter.api.Test;

abstract class AbstractJUnit5Test {

  @Test
  void generalTest() {
    System.out.println("This is a test in the abstract class");
  }

  @Test
  abstract void concreteTest();
}
Run Code Online (Sandbox Code Playgroud)

具体的测试课

import org.junit.jupiter.api.Test;

class ConcreteJUnt5Test extends AbstractJUnit5Test {

  // only gets executed with an additional @Test annotation:
  @Override
  void concreteTest() { 
    System.out.println("This is from the concrete test method.");
  }
}
Run Code Online (Sandbox Code Playgroud)

java junit junit5

6
推荐指数
1
解决办法
2110
查看次数

标签 统计

junit ×2

junit5 ×2

intellij-idea ×1

java ×1

junit4 ×1

maven ×1