java - Mavn执行测试时<scope>test</scope>导致错误
问题描述
学习maven test时,执行mvn test时,会找不到org.junit在pom.xml中已经引入
<dependencies><dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version></dependency><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope></dependency> </dependencies>
报错信息如下
文件目录如下
hello目录下存在如下文件
其中GreeterTest为测试
执行mvn compile 或者mvn package也会报错
当把pom.xml中junit依赖的scope去掉时,编译和测试都能成功。
<dependencies><dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.2</version></dependency><dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version></dependency> </dependencies>
造成这个的原因是什么?maven在执行compile时同时编译*Test的文件吗,那么为什么mvn test也不能成功?mvn test不是会自动执行*Test的文件吗?而且scope test确定了测试时会引入junit
问题解答
回答1:这个问题其实你因为你不熟悉maven文件结构所致.测试类一般是放在src/test/java,而不是放在src/main/java下.maven在编译的时候,src/main/java下是不引用<scope>test</scope>的jar,而编译src/test/java下的测试这会引用<scope>test</scope>的jar
相关文章:
1. 为什么span的color非要内联样式才起作用?2. docker - 如何修改运行中容器的配置3. css - div设置float:left后高度设置自动会无效 ?4. 求大神指点js修改margintop导致无限下滑的问题5. linux - centos 配置ip?6. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?7. 老师,请问我打开browsersync出现这个问题怎么解决啊?8. 请问一下各位老鸟 我一直在学习独孤九贱 现在是在tp5 今天发现 这个系列视频没有实战9. 在cmd下进入mysql数据库,可以输入中文,但是查看表信息,不显示中文,是怎么回事,怎新手,请老师10. html5 - H5做的手机分享页微信更新后,分享出去不再默认显示第一个图 作为缩略图

网公网安备