尝试执行 junit 测试时出错

Error while trying to execute junit tests

我正在尝试 运行 一些 junit 测试,但我经常出错。

我的 pom.xml 具有以下 Maven 依赖项:

<junit.version>4.12</junit.version>
<junit.jupiter.version>5.0.1</junit.jupiter.version>
<junit.vintage.version>${junit.version}.1</junit.vintage.version>
<junit.platform.version>1.0.1</junit.platform.version>

<!-- junit dependencies -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- Only required to run tests in an IDE that bundles an older version -->
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
        <version>${junit.platform.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- Only required to run tests in an IDE that bundles an older version -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>${junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- Only required to run tests in an IDE that bundles an older version -->
    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit.vintage.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- To avoid compiler warnings about @API annotations in JUnit code -->
    <dependency>
        <groupId>org.apiguardian</groupId>
        <artifactId>apiguardian-api</artifactId>
        <version>1.0.0</version>
        <scope>test</scope>
    </dependency>

我实际上有三个方法,带有 @BeforeAll@Before@Test 注释。这些方法只有打印功能才能通过测试。

我遇到的错误如下:

nov 06, 2017 10:36:17 AM org.junit.jupiter.engine.discovery.JavaElementsResolver resolveMethod
    ADVERTENCIA: Method 'public void getTest() throws java.lang.Exception' could not be resolved.
    10:36:18.048 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.tests]
    10:36:18.052 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
    10:36:18.058 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
    10:36:18.064 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.tests] from class [org.springframework.test.context.support.DefaultTestContextBootstrapper]
    10:36:18.086 [main] DEBUG org.springframework.test.context.support.AbstractDelegatingSmartContextLoader - Delegating to AnnotationConfigContextLoader to process context configuration [ContextConfigurationAttributes@68567e20 declaringClass = 'com.tests', classes = '{class com.tests.TestConfig}', locations = '{}', inheritLocations = true, initializers = '{}', inheritInitializers = true, name = [null], contextLoaderClass = 'org.springframework.test.context.ContextLoader'].
    10:36:18.101 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.tests]
    10:36:18.102 [main] DEBUG org.springframework.test.context.support.DefaultTestContextBootstrapper - @TestExecutionListeners is not present for class [com.tests]: using defaults.
    10:36:18.105 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
    10:36:18.117 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
    10:36:18.117 [main] INFO org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@27ff5d15, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4e096385, org.springframework.test.context.support.DirtiesContextTestExecutionListener@649bec2e, org.springframework.test.context.transaction.TransactionalTestExecutionListener@693fe6c9, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@34f5090e]
    10:36:18.121 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.tests]
    10:36:18.121 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.tests]
    10:36:18.151 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.tests]
    10:36:18.151 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.tests]
    Exception in thread "main" java.lang.NoSuchMethodError: org.junit.platform.launcher.Launcher.execute(Lorg/junit/platform/launcher/LauncherDiscoveryRequest;)V
        at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:59)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:237)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我正在使用 IntelliJ IDEA 2016.3.5.

我用谷歌搜索了一下,但没有找到任何好的回复,有什么想法吗?

我已经从您提供的 link 下载了该项目。当我将 add 方法更改为 public 时,一切正常:

@Test
public void add() {
    Calculator calculator = new Calculator();
    assertEquals(2, calculator.add(1, 1), "1 + 1 should equal 2");
}

我尝试 运行 从 IDE 和 maven 进行测试。要从 Maven 进行 运行 测试,您可以尝试在命令行中执行以下命令:

mvn clean install

这将为您构建项目并 运行 测试。

我的设置是:

  • Java 版本:“1.8.0_131”
  • Maven 版本: 3.3.9
  • IntelliJ idea 版本(如果你想 运行 从 IDE 测试可能会有用):2017.2.5