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. css3动画 - css3 animation初始动画卡顿是怎么回事?2. 现在大家是用Mysql还是mariaDb?3. mysql - Access denied for user ’ODBC’@’localhost’ (using password: NO)4. 关于python的继承的一个疑问5. mysql - msql什么叫并发6. 百度地图api - Android百度地图SDK,MapView上层按钮可见却不可触,怎么解决?7. python3.x - 如何将python3.4的程序转为python2.78. javascript - 微信小程序 如何实现这种左滑动出现删除的办法?有相关api吗?9. python2.7为什么点击了"开始"按钮后,tkinter上的按钮,图中红色部分,再也点不动了?10. python - 如何在docker上部署pyspider

网公网安备