scope的分類
compile(編譯范圍)
默認就是compile,什么都不配置也就是意味着compile。compile表示被依賴項目需要參與當前項目的編譯,當然后續的測試,
運行周期也參與其中,是一個比較強的依賴。打包的時候通常需要包含進去。
test(測試范圍)
scope為test表示依賴項目僅僅參與測試相關的工作,包括測試代碼的編譯,執行。比較典型的如junit。
PS: test表示只能在src下的test文件夾下面才可以使用,你如果在a項目中引入了這個依賴,在b項目引入了a項目作為依賴,在b項目中這個注解不會生效,因為scope為test時無法傳遞依賴。
runntime(運行時范圍)
runntime表示被依賴項目無需參與項目的編譯,不過后期的測試和運行周期需要其參與。與compile相比,跳過編譯而已,
說實話在終端的項目(非開源,企業內部系統)中,和compile區別不是很大。比較常見的如JSR×××的實現,對應的API jar是compile的,
具體實現是runtime的,compile只需要知道接口就足夠了。Oracle jdbc驅動架包就是一個很好的例子,一般scope為runntime。
另外runntime的依賴通常和optional搭配使用,optional為true。我可以用A實現,也可以用B實現。
provided(已提供范圍)
provided意味着打包的時候可以不用包進去,別的設施(Web Container)會提供。事實上該依賴理論上可以參與編譯,測試,運行等周期。相當於compile,但是在打包階段做了exclude的動作。
例如, 如果你開發了一個web 應用,你可能在編譯 classpath 中需要可用的Servlet API 來編譯一個servlet,但是你不會想要在打包好的WAR 中包含這個Servlet API;
這個Servlet API JAR 由你的應用服務器或者servlet 容器提供。已提供范圍的依賴在編譯classpath (不是運行時)可用。它們不是傳遞性的,也不會被打包。
system(系統范圍)
system范圍依賴與provided 類似,但是你必須顯式的提供一個對於本地系統中JAR 文件的路徑。這么做是為了允許基於本地對象編譯,
而這些對象是系統類庫的一部分。這樣的構件應該是一直可用的,Maven 也不會在倉庫中去尋找它。如果你將一個依賴范圍設置成系統范圍,
你必須同時提供一個 systemPath 元素。注意該范圍是不推薦使用的(你應該一直盡量去從公共或定制的 Maven 倉庫中引用依賴)。
scope的依賴傳遞
A–>B–>C。當前項目為A,A依賴於B,B依賴於C。知道B在A項目中的scope,那么怎么知道C在A中的scope呢?答案是:
當C是test或者provided時,C直接被丟棄,A不依賴C;
否則A依賴C,C的scope繼承於B的scope。
下面是一張nexus畫的圖。
使用test注解
@runWith注解作用:
--@RunWith就是一個運行器
--@RunWith(JUnit4.class)就是指用JUnit4來運行
--@RunWith(SpringJUnit4ClassRunner.class),讓測試運行於Spring測試環 境,以便在測試開始的時候自動創建Spring的應用上下文
--@RunWith(Suite.class)的話就是一套測試集合
引申:
Spring Boot 1.5.2 Junit測試
使用 Spring 進行單元測試
方法1:
@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @EnableAutoConfiguration public class BBTestAA { @Autowired private TestRestTemplate testRestTemplate; //Application.class 為SpringBoot的啟動入口類,每個SpringBoot項目大家都會配置 }
如果pom.xml中有如下代碼,這行@RunWith(SpringRunner.class)就不會出現SpringRunner,反而有@RunWith(SpringJUnit4ClassRunner.class)
<!--spring-test測試=--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency>
如果pom.xml中沒有這段,則@RunWith(SpringRunner.class)不會報錯。如果有這段:①未注釋<scope>test</scope>會報錯;②注釋<scope>test</scope>不會報錯
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
如果pom.xml中沒有這段,則會報錯。如果有這段:①未注釋<scope>test</scope>SpringRunner、SpringBootTest無法引用,會報錯;②注釋<scope>test</scope>不會報錯
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.9.RELEASE</version> <scope>test</scope> </dependency>
總結起來,想要使用
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
pom.xml中應該引用這兩個
<!--spring-test測試=--> <!--<dependency>--> <!--<groupId>org.springframework</groupId>--> <!--<artifactId>spring-test</artifactId>--> <!--<version>4.2.4.RELEASE</version>--> <!--</dependency>--> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.9.RELEASE</version> <!--<scope>test</scope>--> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!--<scope>test</scope>--> </dependency>
方法2:
如果有<scope>test</scope>@RunWith報紅,沒有<scope>test</scope>會引入該類
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
如果有<scope>test</scope>@SpringBootTest報紅,沒有<scope>test</scope>會引入該類
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <version>1.5.9.RELEASE</version> <scope>test</scope> </dependency>
如果是<version>4.2.4.RELEASE</version>SpringRunner報紅,如果<version>4.2.4.RELEASE</version>會引入該類
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.4.RELEASE</version> </dependency>
所以最后要正確使用,需引入這些架包2.在IDE中新增JunitTest類
@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class @SpringBootTest(classes = Application.class) //1.4版本之前用的是//@SpringApplicationConfiguration(classes = Application.class) public class SystemInfoServiceImplTest { @Autowired private ISystemInfoService systemInfoservice; @Test public void add() throws Exception { } @Test public void findAll() throws Exception { } }
主要是注解的更改,如果注解用的不對,會報各種奇怪的問題,例如applicationContext找不到,datasource實例化失敗等等。
————————————————
版權聲明:本文為CSDN博主「Ian-sheng」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/qq_15028299/article/details/90598404