1. 程式人生 > >springboot 單元測試service遇到的坑(cannot resolve symbol “RunWith” )

springboot 單元測試service遇到的坑(cannot resolve symbol “RunWith” )

springboot專案,對service層進行單元測試的步驟:

1.新增相關依賴,

2.在測試類新增兩個註解:@RunWith(SpringJUnit4ClassRunner.class),

                                        @SpringBootTest(classes = ChargerOperateApplication.class),

                                        新增@Before\@After,注入自己的bean

3.編寫測試程式碼。

具體如下:

1.新增相關依賴,

<!-- 單元測試依賴 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.3.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>1.9.5</version>
            <scope>test</scope>
        </dependency>

注意:junit最好使用4.5以上的版本,否則有可能報錯,或者報cannot resolve symbol runwith。

2.程式碼編寫:
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.junit.runner.RunWith;


/**
 * Created by arvin on 2018/5/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ChargerOperateApplication.class)
public class TestJunit {


    @Autowired
    private NetecoAlarmDataService netecoAlarmDataService;


    @Test
    public void test1() throws Exception {
        String url = "";
        NetecoAlarmData netecoAlarmData = new NetecoAlarmData();
        netecoAlarmData.setAlarmId(4);
        netecoAlarmData.setAlarmName("123");
        netecoAlarmData.setAlarmSerialNo(201805166);
        netecoAlarmData.setGlobalSN(6);
        netecoAlarmData.setCleared(false);
        netecoAlarmData.setClearedTime("2018-05-16 01:10:10");
        netecoAlarmData.setAlarmTime("2018-05-16 01:10:10");
        netecoAlarmData.setDeviceId("2343");
        netecoAlarmData.setLocationInfo("nanjing-jiangning");
        netecoAlarmData.setThresholdInfo("fdsfds");
        netecoAlarmDataService.addNetecoAlarmData(url,"",netecoAlarmData);
    }

}

3.異常出現和解決:

異常:程式碼寫好發現導包失敗,並且報cannot resolve symbol runwith。

解決思路:

1.jar包是否有衝突,如若有,先解決jar包衝突,

2.junit的版本,最好使用4.5以上版本,我用的4.12版本

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

3.重新載入jar包:maven-----reimport all

散步走完後還是報錯!!!!!!!!!

最後直接複製了runwith的包名放進程式碼:import org.junit.runner.RunWith;

參考部落格:https://blog.csdn.net/bobo_12138/article/details/77609109