1. 程式人生 > 實用技巧 >Springboot測試類之@RunWith註解

Springboot測試類之@RunWith註解

@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>

所以最後要正確使用,需引入這些架包

  <!--spring-test測試=-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>1.5.9.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</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例項化失敗等等。

  1. 為了支援上面兩個註解,maven檔案中要用對依賴以及版本,我當時新增SpringRunner.class所在的依賴jar時,由於用了idea的auto-imported,IDE自動匯入了版本是3.x的,實際應該匯入4.x,我一直以為idea匯入的是正確的,導致在這上面費時頗多,後來我手工寫入就解決了。下面是正確的spring boot test的maven依賴