1. 程式人生 > 其它 >Idea中maven專案pom檔案中已引入testng但專案檔案中無法引入@Test

Idea中maven專案pom檔案中已引入testng但專案檔案中無法引入@Test

問題:

1.Idea中maven專案的pom.xml檔案中引入依賴testng

2.此時可以看到已經出現testng的jar包,ERPLogin.java中引用@Test發現無法引用,然後直接import org.testng.annotations.Test也無法顯示

3.但是在test目錄下能引用到testng

原因:

在pom.xml檔案引用testng的時候,有個標籤<scope>test</scope>限制了只能在test目錄下使用

1         <dependency>
2             <groupId>org.testng</groupId>
3             <artifactId>testng</artifactId>
4             <version>6.14.3</version>
5             <scope>test</scope>
6         </dependency>

在POM 4中,<dependency>中引入了<scope>,它主要管理依賴的部署。目前<scope>可以使用5個值:
*compile,預設值,適用於所有階段,會隨著專案一起釋出。
*provided,類似compile,期望JDK、容器或使用者會提供這個依賴。如servlet.jar。
*runtime,只在執行時使用,如JDBC驅動,適用執行和測試階段。
*test,只在測試時使用,用於編譯和執行測試程式碼。不會隨專案釋出。
*system,類似provided,需要顯式提供包含依賴的jar,Maven不會在Repository中查詢它。

解決:

所以只需要將上面pom.xml檔案中的<scope>test</scope>去掉或者改成<scope>compile</scope>

轉自:https://www.cnblogs.com/smartsmile-yxh/p/12286377.html