1、产生的问题:

maven项目执行单元测试可以正常执行,但是执行maven命令test,则报一下异常:

org.apache.maven.plugins:maven-compiler-plugin:2.5.1:testCompile (default-testCompile) on project support: Compilation failure: Compilation failure:
[ERROR] E:\workspace\support\src\test\java\com\test\spring\support\TestSpringContextHolder.java:[7,16] 错误: 程序包org.junit不存在
[ERROR] E:\workspace\support\src\test\java\com\test\spring\support\TestSpringContextHolder.java:[16,2] 错误: 找不到符号
[ERROR] 符号:   类 Test
[ERROR] 位置: 类 TestSpringContextHolder
[ERROR] E:\workspace\support\src\test\java\com\test\spring\support\TestSpringContextHolder.java:[29,2] 错误: 找不到符号
[ERROR] 符号:   类 Before
[ERROR] 位置: 类 TestSpringContextHolder
[ERROR] E:\workspace\support\src\test\java\com\test\spring\support\TestSpringContextHolder.java:[43,5] 错误: 找不到符号




2、产生上述错误的原因:

1)、maven项目,单元测试和运行使用不同的classpath,测试使用test-classpath,运行使用classpath;
2)、创建maven项目使用junit默认的版本为3.8.1,它使用的是编程的方式,即使maven dependencies目录引入了junit,但也无法使用,只能通过build path添加junit支持

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

这里写图片描述




3、解决方案:

将junit的版本提高到4.x,配置代码如下:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!-- 默认的版本为3.8.1,修改为4.x,因为3.x使用的为编程的方式,4.x为注解的形式。 -->
            <version>4.11</version>
            <scope>test</scope>
        </dependency>




4、补充:我上面贴出的异常目录位于。。。\src\test\java\。。。,不是。。。\src\main\java\。。。,处理方式如上,如果异常发生在main目录而不是test目录,同时junit版本是4.x,解决方案则将scope属性去掉:

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <!-- 默认的版本为3.8.1,修改为4.x,因为3.x使用的为编程的方式,4.x为注解的形式。 -->
            <version>4.11</version>
            <!-- 去掉scope作用域,使用默认的compile,编译、测试、运行都有效的作用域 -->
            <!--<scope>test</scope>-->
        </dependency>

解决思路参考:http://www.cnblogs.com/LiuChunfu/p/5598367.html

GitHub 加速计划 / ma / maven
36
3
下载
Maven: Apache Maven是一个开源的项目管理和构建工具,主要用于Java项目。适合需要自动化项目构建和依赖管理的开发者。特点包括约定优于配置、易于使用和社区驱动。
最近提交(Master分支:3 个月前 )
92a8fcb7 Bumps [org.codehaus.mojo:exec-maven-plugin](https://github.com/mojohaus/exec-maven-plugin) from 3.6.2 to 3.6.3. - [Release notes](https://github.com/mojohaus/exec-maven-plugin/releases) - [Commits](https://github.com/mojohaus/exec-maven-plugin/compare/3.6.2...3.6.3) --- updated-dependencies: - dependency-name: org.codehaus.mojo:exec-maven-plugin dependency-version: 3.6.3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> 2 天前
2474702f Bumps [org.codehaus.mojo:buildnumber-maven-plugin](https://github.com/mojohaus/buildnumber-maven-plugin) from 3.2.1 to 3.3.0. - [Release notes](https://github.com/mojohaus/buildnumber-maven-plugin/releases) - [Commits](https://github.com/mojohaus/buildnumber-maven-plugin/compare/3.2.1...buildnumber-maven-plugin-3.3.0) --- updated-dependencies: - dependency-name: org.codehaus.mojo:buildnumber-maven-plugin dependency-version: 3.3.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 2 天前
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐