V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
palmers
V2EX  ›  程序员

请教:单元测试运行找不到类问题

  •  
  •   palmers · 2021-08-19 19:46:33 +08:00 · 1195 次点击
    这是一个创建于 974 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码目录结构是这样的:

    ├─src
    │  ├─main
    │  │  └─java
    │  │      └─com
    │  │          └─xx  Sample.java
    │  └─test
    │      └─java
    │          └─com
    │              └─xx SampleTest.java
    
    

    设置 src/main/java 目录为 Source root, src/test/java 为 test source root SampleTest.java 测试类中有调用 Sample 类中的方法 当我运行 SampleTest 单元测试方法时,提示找不到 com.xx.Sample.java 请问 这是为什么呢?依赖项目其他模块代码也是找不到类 在 IDEA 中,不会提示编译错误,只有运行时,在 testCompile 阶段提示找不到类,在跳过测试构建是正常的 运行测试使用的本地 maven 仓库已经指向了我使用的本地仓库(从 debug 日志中看到)

    请大家帮帮我 谢谢大家了

    第 1 条附言  ·  2021-08-20 10:40:44 +08:00

    问题已经解决了!谢谢大家! 原因: maven插件maven-compiler-plugin配置问题导致 插件maven-compiler-plugin 配置

    <configuration>
        <skipMain>true</skipMain>
    </configuration>
    

    我专门查询了插件 maven-compiler-plugin 该配置,是为了不编译main sources 文档: https://maven.apache.org/plugins/maven-compiler-plugin/compile-mojo.html

    <skipMain>	boolean	2.0	Set this to 'true' to bypass compilation of main sources. Its use is NOT 
                                                  RECOMMENDED, but quite convenient on occasion.
                                                 User property is: maven.main.skip.
    

    至此,问题得到解决,由于自己对maven项目结构规范和使用插件以及maven插件生命周期不够熟悉,导致这期间我尝试修改test目录结构和添加pom配置 都是围绕 test来做 所以一直没能解决问题

    9 条回复    2021-08-20 08:53:06 +08:00
    sutra
        1
    sutra  
       2021-08-19 19:48:45 +08:00
    贴出 mvn test 的输出大概会简单点。
    palmers
        2
    palmers  
    OP
       2021-08-19 20:04:51 +08:00
    源码:
    palmers
        3
    palmers  
    OP
       2021-08-19 20:06:30 +08:00
    ```java
    public class MethClazzTest {

    @Test
    public void sayHello() {
    new MethClazz().sayHello();
    }
    }
    ```

    ```
    $ mvn test
    [INFO] Scanning for projects...
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Build Order:
    [INFO]
    [INFO] unit-testing [pom]
    [INFO] test-module [jar]
    [INFO]
    [INFO] ----------------------< org.example:unit-testing >----------------------
    [INFO] Building unit-testing 1.0-SNAPSHOT [1/2]
    [INFO] --------------------------------[ pom ]---------------------------------
    [INFO]
    [INFO] ----------------------< org.example:test-module >-----------------------
    [INFO] Building test-module 1.0-SNAPSHOT [2/2]
    [INFO] --------------------------------[ jar ]---------------------------------
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test-module ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] Copying 0 resource
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ test-module ---
    [INFO] Not compiling main sources
    [INFO]
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test-module ---
    [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
    [INFO] skip non existing resourceDirectory E:\projects\unit-testing\test-module\src\test\resources
    [INFO]
    [INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ test-module ---
    [INFO] Changes detected - recompiling the module!
    [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
    [INFO] Compiling 1 source file to E:\projects\unit-testing\test-module\target\test-classes
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR :
    [INFO] -------------------------------------------------------------
    [ERROR] /E:/projects/unit-testing/test-module/src/test/java/org/example/d1/MethClazzTest.java:[14,13] 找不到符号
    符号: 类 MethClazz
    位置: 类 org.example.d1.MethClazzTest
    [INFO] 1 error
    [INFO] -------------------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] Reactor Summary:
    [INFO]
    [INFO] unit-testing 1.0-SNAPSHOT .......................... SUCCESS [ 0.040 s]
    [INFO] test-module 1.0-SNAPSHOT ........................... FAILURE [ 3.981 s]
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 4.484 s
    [INFO] Finished at: 2021-08-19T20:03:02+08:00
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:testCompile (default-testCompile) on project test-module: Compilation failure
    [ERROR] /E:/projects/unit-testing/test-module/src/test/java/org/example/d1/MethClazzTest.java:[14,13] 找不到符号
    [ERROR] 符号: 类 MethClazz
    [ERROR] 位置: 类 org.example.d1.MethClazzTest
    [ERROR]
    [ERROR] -> [Help 1]
    [ERROR]
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR]
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
    [ERROR]
    [ERROR] After correcting the problems, you can resume the build with the command
    [ERROR] mvn <goals> -rf :test-module

    ```
    palmers
        4
    palmers  
    OP
       2021-08-19 20:07:02 +08:00
    @sutra 日志就是上面这样的
    sutra
        5
    sutra  
       2021-08-19 20:50:45 +08:00
    pom.xml 贴出来看看。
    sutra
        6
    sutra  
       2021-08-19 20:51:48 +08:00
    [INFO] unit-testing 1.0-SNAPSHOT .......................... SUCCESS [ 0.040 s]
    [INFO] test-module 1.0-SNAPSHOT ........................... FAILURE [ 3.981 s]

    你这好像放在两个 maven project 里,然后没有写依赖吧?
    palmers
        7
    palmers  
    OP
       2021-08-20 08:46:09 +08:00
    @sutra unit-testing 是 test-module 的父模块 然后单元测试在 test-module 模块中
    palmers
        8
    palmers  
    OP
       2021-08-20 08:47:42 +08:00
    ```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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>unit-testing</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
    <module>test-module</module>
    </modules>

    <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
    </dependency>
    </dependencies>

    <build>
    <pluginManagement>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.8.0</version>
    <configuration>
    <skipMain>true</skipMain>
    </configuration>
    </plugin>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.22.2</version>
    </plugin>
    <plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.8.2</version>
    <executions>
    <execution>
    <goals>
    <goal>prepare-agent</goal>
    </goals>
    </execution>
    <!-- attached to Maven test phase -->
    <execution>
    <id>report</id>
    <phase>test</phase>
    <goals>
    <goal>report</goal>
    </goals>
    </execution>
    </executions>
    </plugin>
    </plugins>
    </pluginManagement>
    </build>
    </project>
    ```
    palmers
        9
    palmers  
    OP
       2021-08-20 08:53:06 +08:00
    test-module 模块
    ```xml
    <parent>
    <artifactId>unit-testing</artifactId>
    <groupId>org.example</groupId>
    <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-module</artifactId>

    <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
    <scope>test</scope>
    </dependency>
    </dependencies>
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2449 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 15:55 · PVG 23:55 · LAX 08:55 · JFK 11:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.