development

Maven이 실행할 JUnit 테스트를 찾지 못함

big-blog 2020. 2. 26. 07:57
반응형

Maven이 실행할 JUnit 테스트를 찾지 못함


나는 maven 프로그램을 가지고 있으며, 잘 컴파일됩니다. 내가 실행할 때 mvn test테스트가 실행 되지 않습니다 (테스트 헤더 아래에 표시 There are no tests to run.).

나는이 문제를으로 실행할 때 출력뿐만 아니라 아래에 포함시킬 매우 간단한 설정으로 다시 만들었습니다 -X.

단위 테스트는 일식에서 잘 실행됩니다 (기본 junit 패키지와 함께 maven이 다운로드 한 junit.jar을 대신 포함 할 때). 또한 mvn test-compile은 테스트 클래스 아래에 클래스를 올바르게 작성합니다. Maven 3.0.2 및 java 1.6.0_24와 함께 OSX 10.6.7에서 이것을 실행하고 있습니다.

디렉토리 구조는 다음과 같습니다.

/my_program/pom.xml
/my_program/src/main/java/ClassUnderTest.java
/my_program/src/test/java/ClassUnderTestTests.java

pom.xml :

<?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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my_group</groupId>
    <artifactId>my_program</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>My Program</name>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

ClassUnderTest.java :

public class ClassUnderTest {

    public int functionUnderTest(int n) {
        return n;
    }

}

ClassUnderTestTests.java :

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ClassUnderTestTests {

    private ClassUnderTest o;

    @Before
    public void setUp() {
        o = new ClassUnderTest();
    }

    @Test
    public void testFunctionUnderTest_testCase1() {
        Assert.assertEquals(1, o.functionUnderTest(1));
    }

    @Test
    public void testFunctionUnderTest_testCase2() {
        Assert.assertEquals(2, o.functionUnderTest(2));
    }
}

mvn -X 테스트 종료 :

[DEBUG] Configuring mojo org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-surefire-plugin:2.7.1, parent: sun.misc.Launcher$AppClassLoader@5224ee]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-surefire-plugin:2.7.1:test' with basic configurator -->
[DEBUG]   (s) basedir = /Users/aaron/Programs/my_program
[DEBUG]   (s) childDelegation = false
[DEBUG]   (s) classesDirectory = /Users/aaron/Programs/my_program/target/classes
[DEBUG]   (s) disableXmlReport = false
[DEBUG]   (s) enableAssertions = true
[DEBUG]   (s) forkMode = once
[DEBUG]   (s) junitArtifactName = junit:junit
[DEBUG]   (s) localRepository =        id: local
      url: file:///Users/aaron/.m2/repository/
   layout: none

[DEBUG]   (f) parallelMavenExecution = false
[DEBUG]   (s) pluginArtifactMap = {org.apache.maven.plugins:maven-surefire-plugin=org.apache.maven.plugins:maven-surefire-plugin:maven-plugin:2.7.1:, org.apache.maven.surefire:surefire-booter=org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile, org.apache.maven.surefire:surefire-api=org.apache.maven.surefire:surefire-api:jar:2.7.1:compile, org.apache.maven.surefire:maven-surefire-common=org.apache.maven.surefire:maven-surefire-common:jar:2.7.1:compile, org.apache.maven.shared:maven-common-artifact-filters=org.apache.maven.shared:maven-common-artifact-filters:jar:1.3:compile, org.codehaus.plexus:plexus-utils=org.codehaus.plexus:plexus-utils:jar:2.0.5:compile, junit:junit=junit:junit:jar:3.8.1:compile, org.apache.maven.reporting:maven-reporting-api=org.apache.maven.reporting:maven-reporting-api:jar:2.0.9:compile}
[DEBUG]   (s) printSummary = true
[DEBUG]   (s) project = MavenProject: my_group:my_program:1.0-SNAPSHOT @ /Users/aaron/Programs/my_program/pom.xml
[DEBUG]   (s) projectArtifactMap = {junit:junit=junit:junit:jar:4.8.1:test}
[DEBUG]   (s) redirectTestOutputToFile = false
[DEBUG]   (s) remoteRepositories = [       id: central
      url: http://repo1.maven.org/maven2
   layout: default
snapshots: [enabled => false, update => daily]
 releases: [enabled => true, update => never]
]
[DEBUG]   (s) reportFormat = brief
[DEBUG]   (s) reportsDirectory = /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG]   (s) session = org.apache.maven.execution.MavenSession@dfbb43
[DEBUG]   (s) skip = false
[DEBUG]   (s) skipTests = false
[DEBUG]   (s) testClassesDirectory = /Users/aaron/Programs/my_program/target/test-classes
[DEBUG]   (s) testFailureIgnore = false
[DEBUG]   (s) testNGArtifactName = org.testng:testng
[DEBUG]   (s) testSourceDirectory = /Users/aaron/Programs/my_program/src/test/java
[DEBUG]   (s) trimStackTrace = true
[DEBUG]   (s) useFile = true
[DEBUG]   (s) useManifestOnlyJar = true
[DEBUG]   (s) workingDirectory = /Users/aaron/Programs/my_program
[DEBUG] -- end configuration --
[INFO] Surefire report directory: /Users/aaron/Programs/my_program/target/surefire-reports
[DEBUG] Setting system property [user.dir]=[/Users/aaron/Programs/my_program]
[DEBUG] Setting system property [localRepository]=[/Users/aaron/.m2/repository]
[DEBUG] Setting system property [basedir]=[/Users/aaron/Programs/my_program]
[DEBUG] Using JVM: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-junit4:jar:2.7.1:test (selected for test)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.7.1:test (selected for test)
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-junit4/2.7.1/surefire-junit4-2.7.1.jar Scope: test
[DEBUG] Adding to surefire test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: test
[DEBUG] Test Classpath :
[DEBUG]   /Users/aaron/Programs/my_program/target/test-classes
[DEBUG]   /Users/aaron/Programs/my_program/target/classes
[DEBUG]   /Users/aaron/.m2/repository/junit/junit/4.8.1/junit-4.8.1.jar
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for /Users/aaron/.m2/repository
[DEBUG] dummy:dummy:jar:1.0 (selected for null)
[DEBUG]   org.apache.maven.surefire:surefire-booter:jar:2.7.1:compile (selected for compile)
[DEBUG]     org.apache.maven.surefire:surefire-api:jar:2.7.1:compile (selected for compile)
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-booter/2.7.1/surefire-booter-2.7.1.jar Scope: compile
[DEBUG] Adding to surefire booter test classpath: /Users/aaron/.m2/repository/org/apache/maven/surefire/surefire-api/2.7.1/surefire-api-2.7.1.jar Scope: compile
Forking command line: /bin/sh -c cd /Users/aaron/Programs/my_program && /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java -jar /Users/aaron/Programs/my_program/target/surefire/surefirebooter6118081963679415631.jar /Users/aaron/Programs/my_program/target/surefire/surefire4887918564882595612tmp /Users/aaron/Programs/my_program/target/surefire/surefire9012255138269731406tmp

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
There are no tests to run.

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.089s
[INFO] Finished at: Mon May 30 12:03:09 EDT 2011
[INFO] Final Memory: 7M/62M
[INFO] ------------------------------------------------------------------------

기본적으로 Maven은 실행할 테스트를 찾을 때 다음과 같은 명명 규칙을 사용합니다.

  • Test*
  • *Test
  • *TestCase

테스트 클래스는 이러한 규칙을 따르지 않습니다. 테스트 클래스에 다른 패턴을 사용 하도록 이름을 바꾸거나 Maven Surefire 플러그인구성 해야합니다 .


또한 단위 테스트 코드가 테스트 폴더 아래에 있어야한다는 것을 알았습니다. 메인 폴더 아래에 넣으면 테스트 클래스로 인식 할 수 없습니다. 예.

잘못된

/my_program/src/main/java/NotTest.java

권리

/my_program/src/test/java/MyTest.java

모듈의 패키징이 올바르게 선언되지 않은 경우 Maven이 테스트를 찾지 못하게 할 수있는 또 다른 사항.

최근에는 누군가가 <packaging>pom</packaging>있었고 내 테스트가 실행되지 않았습니다. 나는 그것을 바꾸었고 <packaging>jar</packaging>지금은 잘 작동합니다.


최신 정보:

@scottyseus가 의견에서 말했듯이 Maven Surefire 2.22.0부터 다음과 같이 충분합니다.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.1</version>
</plugin>

JUnit 5를 사용할 때 동일한 문제가 발생했습니다. Maven Surefire는 JUnit 5 테스트를 실행하기위한 플러그인이 필요합니다. 이것을 우리에게 추가하십시오 pom.xml:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <dependencies>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.2.0-M1</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.2.0-M1</version>
        </dependency>
    </dependencies>
</plugin>

출처 : https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven


또한 테스트 클래스 디렉토리 (예 : src / test / java)가 <testSourceDirectory>pom.xml의 <build>property 에서 property 나열된 디렉토리와 일치하는지 확인 하십시오 . 그것을 찾기 위해 시간이 걸렸습니다.


프로젝트가 있다면 Maven은 테스트를 실행하지 않습니다. <packaging>pom</packaging>

테스트를 실행하려면 패키징을 jar (또는 다른 Java 아티팩트 유형)로 설정해야합니다. <packaging>jar</packaging>


확인하십시오 (jUnit-4.12 및 Eclipse surefire 플러그인의 경우)

  1. POM.xml에 필요한 jUnit 버전을 종속성으로 추가하십시오. Maven-> 프로젝트 업데이트를 수행하여 프로젝트에서 내 보낸 필요한 jar을보십시오.
  2. 테스트 클래스는 src / test / java 폴더와이 폴더의 하위 디렉토리 아래에 있습니다 (또는 기본 폴더는 config testSourceDirectory의 POM에서 지정할 수 있음). 클래스 이름에는 'Test'라는 꼬리 단어가 있어야합니다.
  3. 테스트 클래스의 테스트 메소드에는 주석 @Test가 있어야합니다.

테스트 앞에 'Abstract'를 붙이면 기본적으로 무시됩니다.


이러한 답변 중 많은 부분이 과거에는 나에게 매우 유용했지만 나중에 다른 사람들에게 도움이 될 수 있으므로 시간이 많이 걸리는 추가 시나리오를 추가하고 싶습니다.

테스트 클래스와 메소드가 공개되어 있는지 확인하십시오.

내 문제는 내 IDE (IntelliJ)의 자동 테스트 클래스 / 메소드 생성 기능을 사용하고 있었고 어떤 이유로 패키지 전용으로 만들었습니다. 나는 이것이 예상보다 놓치기 쉽다는 것을 알았습니다.


공유 Java / Groovy 애플리케이션이 있고 Groovy 단위 테스트 만 있으면 Maven에서 테스트를 찾지 않습니다. src / test / java에서 하나의 단위 테스트를 추가하여이를 해결할 수 있습니다.


testng 의존성 이이 문제를 일으키는 것으로 밝혀진 후 비슷한 문제가 발생했습니다. pom에서 testng 종속성을 제거한 후 (더 이상 필요하지 않기 때문에) 제대로 작동하기 시작했습니다.

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8</version>
        <scope>test</scope>
    </dependency>

필자의 경우 이전 버전의 JUnit 테스트와 호환되고 실행할 수있는 junit-vintage-engine을 추가했습니다. JUnit 5를 사용하고 있습니다.

<dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <scope>test</scope>
</dependency>

/my_program/src/test/java/ClassUnderTestTests.java

해야한다

/my_program/src/test/java/ClassUnderTestTest.java

Maven은 Test가 끝나는 것을 찾거나 Test로 시작하여 자동으로 실행됩니다.

그러나 당신은 사용할 수 있습니다

mvn surefire:test -Dtest=ClassUnderTestTests.java 

테스트를 실행하십시오.


테스트 클래스 이름이 표준 명명 규칙을 따르지 않으면 (위의 @axtavt로 강조 표시됨) pom.xmlMaven에서 테스트를 선택하기 위해 패턴 / 클래스 이름을 추가해야합니다.

...
<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*_UT.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build> 
...

나는 같은 문제에 직면하여 pom.xml의 아래 변경으로 해결되었습니다.

<build>
    <testSourceDirectory>test</testSourceDirectory>

...

로 변경:

<build>
    <testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>

테스트 케이스를 실행하지 않는 또 다른 이유는 나에게 일어났다. 나는 완전히 다른 목적을 위해 "test"라는 이름의 속성을 가졌지 만 확실한 플러그인을 방해했다. 따라서 POM에서 다음을 확인하십시오.

<properties>
  <test>.... </test>
  ...
</properties>

제거하십시오.


하나의 팁 (이전 답변 외에도) :

Eclipse에서 프로젝트의 속성으로 이동하여 다음을 클릭하십시오 Run/Debug Settings.

"이 페이지에서는 현재 선택된 리소스로 시작 구성을 관리 할 수 ​​있습니다"

거기에서 프로젝트 ( src/test/java폴더 또는 코스 아래)에있는 모든 JU (JUnit) 테스트를 추가 (New ...) 또는 제거 (Delete) 할 수 있습니다 .


내 pom.xml에 추가 해야하는 정확한 코드는 다음과 같습니다.

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>1.2.0-M1</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>5.2.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

그리고 여기 내 의존성이 있습니다.

    <dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.easytesting</groupId>
        <artifactId>fest-assert-core</artifactId>
        <version>2.0M10</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-surefire-provider</artifactId>
        <version>1.2.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0-M1</version>
    </dependency>
</dependencies>

JUnit 4에서 테스트를 작성하고 JUnit 5 종속성을 surefire 플러그인에 추가 한 경우 테스트가 실행되지 않습니다.

이 경우 surefire 플러그인에서 JUnit 5 종속성을 주석 처리하십시오.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <!--<dependencies>-->
                <!--<dependency>-->
                    <!--<groupId>org.junit.platform</groupId>-->
                    <!--<artifactId>junit-platform-surefire-provider</artifactId>-->
                    <!--<version>1.0.0</version>-->
                <!--</dependency>-->
                <!--<dependency>-->
                    <!--<groupId>org.junit.jupiter</groupId>-->
                    <!--<artifactId>junit-jupiter-engine</artifactId>-->
                    <!--<version>${junit.version}</version>-->
                <!--</dependency>-->
            <!--</dependencies>-->
        </plugin>

다음은 Junit 5에서 잘 작동했습니다.

https://junit.org/junit5/docs/current/user-guide/#running-tests-build-maven

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
    </plugins>
</build>
<!-- ... -->
<dependencies>
    <!-- ... -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>
    <!-- ... -->
</dependencies>
<!-- ... -->

junitArtifactName사용중인 JUnit이 표준 ( junit:junit)이 아니라 예를 들어 ...

<dependency>
    <groupId>org.eclipse.orbit</groupId>
    <artifactId>org.junit</artifactId>
    <version>4.11.0</version>
    <type>bundle</type>
    <scope>test</scope>
</dependency>

누군가가 검색했지만 해결하지 못하면 다른 테스트를위한 라이브러리가 있습니다.

<dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${org.junit.jupiter.version}</version>
        <scope>test</scope>
    </dependency>

junit을 설치할 때 모든 것이 효과가 있었으면 좋겠다.

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

나는이 코드를 사용했다

<sourceDirectory>src_controller</sourceDirectory>
  <testSourceDirectory>src_test</testSourceDirectory>

내 pom.xml에 testng 파일이 있는지 확인하십시오.

<suiteXmlFile>/Users/mac/xxx/xxx/xx.xxxx.xx/xxx.restassured.xx/testng.xml</suiteXmlFile>

JUnit5에서 surfire plugin 3.x.x +를 사용하고 실수로 JUnit4의 주석으로 테스트 클래스에 @Test주석을 달 때 이러한 문제가 발생할 수 있습니다 .

사용 : org.junit.jupiter.api.Test(Junit4) 대신 org.junit.Test(JUnit5)

참고 : IDE가 JUnit4 테스트와 같이이 문제를 실행할 수 있으므로 눈에 띄지 않을 수 있습니다.


쉽게 간과되는 또 다른 문제-클래스 파일의 확장자가 .java인지 확인하십시오.


필자의 경우 다중 모듈 응용 프로그램을 Spring Boot로 마이그레이션합니다. 불행히도 maven은 더 이상 모듈에서 모든 테스트를 실행하지 않았습니다. 테스트 클래스의 이름은 바뀌지 않았습니다. 우리는 이름 지정 규칙을 따릅니다.

결국 surefire-junit47플러그인에 종속성 추가했을 때 도움이되었습니다 maven-surefire-plugin. 그러나 나는 왜 시행 착오인지 설명 할 수 없었습니다.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<dependencies>
  <dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit47</artifactId>
    <version>${maven-surefire-plugin.version}</version>
  </dependency>
</dependencies>


이 문제로 어려움을 겪고 있습니다. 제 경우에는 올바른 @Test 주석을 가져 오지 않았습니다 .

1) @Test가 org.junit.jupiter.api.Test 에서 온 것인지 확인하십시오 (Junit 5를 사용하는 경우).

2)를 Junit5 대신 @RunWith(SpringRunner.class)사용@ExtendWith(SpringExtension.class)

import org.junit.jupiter.api.Test;

@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application.properties")    
public class CotacaoTest {
    @Test
    public void testXXX() {

    }
}

참고 URL : https://stackoverflow.com/questions/6178583/maven-does-not-find-junit-tests-to-run



반응형