简体   繁体   English

无法运行测试 intelliJ - Spring 项目 - 错误:java.lang.NoSuchMethodError

[英]Cannot run tests intelliJ - Spring project - error: java.lang.NoSuchMethodError

Im working on restful application.我正在研究宁静的应用程序。 At the beginning I used TDD methodology.一开始我使用了 TDD 方法。 On the second stage I switch to the option: tests after implemented methods.在第二阶段,我切换到选项:在实施方法后进行测试。 But I cannot run a test.但我无法运行测试。 Type of error:错误类型:

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.

POM file: POM文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.przemyslawostrouch</groupId>
    <artifactId>weatherapp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>weatherapp</name>
    <description>Web project will aviation weather and Notam informations</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>RELEASE</version>
        </dependency><!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20170516</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.1</version>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
            <scope>provided</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.squareup.retrofit/retrofit -->
        <dependency>
            <groupId>com.squareup.retrofit</groupId>
            <artifactId>retrofit</artifactId>
            <version>1.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Test class example:测试类示例:

    package com.przemyslawostrouch.parsers;

import com.przemyslawostrouch.domain.Airport;
import com.przemyslawostrouch.domain.City;
import com.przemyslawostrouch.domain.CityAirportDistance;
import com.przemyslawostrouch.logic.NearestAirport;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;


/**
 * Created by Przemek on 2017-06-24.
 */
class NearestAirportTest {

    private NearestAirport nearestAirport;
    private City katowiceCity;
    private Airport katowiceAirport;
    private Airport frankfurtAirport;
    private Airport pulkovoAirport;
    private CityAirportDistance cityAirportDistance;
    private CityAirportDistance cityAirportDistance1;
    private CityAirportDistance cityAirportDistance2;
    private String searchedCityUpperCase;

    public void setup() {


        searchedCityUpperCase = "Katowice";

        nearestAirport = new NearestAirport();

        katowiceCity = City.builder()
                .name("Katowice")
                .latitude(50.26489189999999)
                .longitude(19.0237815)
                .build();

        katowiceAirport = Airport.builder()
                .name("Muchowiec Airport")
                .longitude(19.03420066833496)
                .latitude(50.23809814453125)
                .build();

        frankfurtAirport = Airport.builder()
                .name("Frankfurt am Main International Airport")
                .longitude(8.5705556)
                .latitude(50.0333333)
                .build();

        pulkovoAirport = Airport.builder()
                .name("Pulkovo Airport")
                .longitude(30.262500762939453)
                .latitude(59.80030059814453)
                .build();

        cityAirportDistance = CityAirportDistance.builder()
                .airportName(katowiceAirport.getName())
                .searchedCityName(searchedCityUpperCase)
                .distanceBetweenSearchedCityAndAirport(1.6565764398773335)
                .build();

        cityAirportDistance1 = CityAirportDistance.builder()
                .airportName(frankfurtAirport.getName())
                .searchedCityName(searchedCityUpperCase)
                .distanceBetweenSearchedCityAndAirport(401.81041784523916)
                .build();

        cityAirportDistance2 = CityAirportDistance.builder()
                .airportName(pulkovoAirport.getName())
                .searchedCityName(searchedCityUpperCase)
                .distanceBetweenSearchedCityAndAirport(688.4011751978946)
                .build();
    }

    @Test
    void isNearestAirportNotNull() {
        setup();
        assertThat(nearestAirport).isNotNull();
    }

    @Test
    void whenTypedCityReturnCityWithFirstLetterUpperCase() {
        setup();
        String searchedCity = "katowice";
        assertThat(nearestAirport.searchedCityToUpperCase(searchedCity)).isEqualTo("Katowice");
    }


    @Test
    void forGivenCityAndAerodromeCoordinatesReturnDistance() {
        setup();
        //City Gdansk
        City gdanskCity = City.builder()
                .name("Gdansk")
                .latitude(54.35202520000001)
                .longitude(18.6466384)
                .build();
        Airport gdanskAirport = Airport.builder()
                .name("Gdańsk Lech Wałęsa Airport")
                .latitude(54.377601623535156)
                .longitude(18.46619987487793)
                .build();

        assertThat(nearestAirport.calculateDistance(gdanskCity, gdanskAirport)).isEqualTo(6.491638306515884);
    }

    @Test
    void forGivenCityNameCalculateDistanceBetweenAllAirports() {
        setup();
        List<Airport> listOfAirportsForTest = new ArrayList<>();

        listOfAirportsForTest.add(katowiceAirport);
        listOfAirportsForTest.add(frankfurtAirport);
        listOfAirportsForTest.add(pulkovoAirport);


        assertThat(nearestAirport.calculateDistanceBetweenAllAirportsAndSelectedCity(listOfAirportsForTest, katowiceCity))
                .contains(cityAirportDistance, cityAirportDistance1, cityAirportDistance2);

    }

    @Test
    void sortListDistancesAndReturnNearestAirport() {
        setup();

        List<CityAirportDistance> cityAirportDistancesListTest = new ArrayList<>();


        cityAirportDistancesListTest.add(cityAirportDistance);
        cityAirportDistancesListTest.add(cityAirportDistance1);
        cityAirportDistancesListTest.add(cityAirportDistance2);

        assertThat(nearestAirport.createListOfNearestAirports(katowiceCity)).contains(cityAirportDistancesListTest.get(0)
                , cityAirportDistancesListTest.get(1)
                , cityAirportDistancesListTest.get(2));

    }

I tried change dependencies, delete these tests and write another, it doesn't work.我尝试更改依赖项,删除这些测试并编写另一个,它不起作用。 I have never met this error, cannot run any test in this project.我从来没有遇到过这个错误,无法在这个项目中运行任何测试。 I cannot find the solution of this problem on stack overflow and similar pages.我无法在堆栈溢出和类似页面上找到此问题的解决方案。 if you need any addition information just ask, thanks for help如果您需要任何附加信息,请询问,谢谢您的帮助

The answer is in your pom.xml file.答案在您的pom.xml文件中。 If you take a look at the console output you will find that IntelliJ IDEA is trying to use JUnit5 runner:如果您查看控制台输出,您会发现 IntelliJ IDEA 正在尝试使用 JUnit5 运行程序:

at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:43)

According to your pom.xml file you expect to use JUnit:4.12 .根据您希望使用JUnit:4.12 pom.xml文件。 Next step is to find which dependency causes this conflict.下一步是找出导致此冲突的依赖项。 Comparing all dependencies listed in pom.xml you can find junit-jupiter-api比较pom.xml列出的所有依赖项,您可以找到junit-jupiter-api

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>RELEASE</version>
    </dependency>

This module is designed for JUnit5 - https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api这个模块是为 JUnit5 设计的 - https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api

Also correct the import of @Test annotation in your test class - you're importing one from jupiter api.还要更正测试类中@Test注释的导入 - 您正在从 jupiter api 导入一个。

EDIT: Last one thing.编辑:最后一件事。 When you face similar issues (egclassloader complains about non-existing methods reference) using Maven's dependency plugin is always a good starting point ( https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts-using-the-dependency-tree.html ).当您遇到类似问题(例如classloader 抱怨不存在的方法引用)时,使用Maven 的依赖插件始终是一个很好的起点( https://maven.apache.org/plugins/maven-dependency-plugin/examples/resolving-conflicts- using-the-dependency-tree.html )。 It will help you in finding which dependency causes conflict.它将帮助您找到导致冲突的依赖项。

This was a nightmare issue even for someone who used IntelliJ for 8 or so years.即使对于使用 IntelliJ 8 年左右的人来说,这也是一个噩梦。 Maven build was clean but somehow IntelliJ was using an old cached dependency during runtime. Maven 构建是干净的,但不知何故 IntelliJ 在运行时使用了旧的缓存依赖项。

I had to reimport each of the submodule (I have a couple of nested ones) to get this working.我必须重新导入每个子模块(我有几个嵌套的)才能使其正常工作。 Right Click on the project and select Maven->Reimport右键单击项目并选择 Maven->Reimport

Below were my failed attempts/debugging, just in case if people haven't tried.以下是我失败的尝试/调试,以防万一人们还没有尝试过。 Hoping some of these could help others.希望其中一些可以帮助其他人。

  1. mvn clean -Dtest=V1RequestMapperTest test so from command line test worked fine so something wrong with IntelliJ mvn clean -Dtest=V1RequestMapperTest test所以从命令行测试工作正常所以 IntelliJ 有问题
  2. Invalidate Cache使缓存失效
  3. Invalidate Cache and Restart使缓存无效并重新启动
  4. Deleted offending dependency from .m2 folder to bring new从 .m2 文件夹中删除了有问题的依赖项以带来新的

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM