【问题标题】:Error while trying to execute junit tests尝试执行junit测试时出错
【发布时间】:2017-11-06 09:51:12
【问题描述】:

我正在尝试运行一些 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。

我在谷歌上搜索过,但没有找到任何好的回应,有什么想法吗?

【问题讨论】:

  • 能否在 GitHub 上提供示例项目,以便更容易找出问题所在?
  • 我创建了这个repository 以复制错误。

标签: java maven intellij-idea junit junit5


【解决方案1】:

我已经从您提供的链接下载了该项目。当我将 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

【讨论】:

  • 看来将我的 IntelliJ idea 版本升级到 2017.2.5 可以解决问题。谢谢!
猜你喜欢
  • 2013-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 2011-12-03
  • 2019-01-29
相关资源
最近更新 更多