Maven之scope詳解 以及test測試

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畫的圖。 
依賴傳遞時scope的計算

使用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實例化失敗等等。


出處:https://www.jianshu.com/p/53da42731e69
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章