Spring 整合 Junit

第一步:導包

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.5.RELEASE</version>
            <scope>test</scope>
        </dependency>

第二步:使用@RunWith 註解替換原有運行器

@RunWith(SpringJUnit4ClassRunner.class)

第三步:使用@ContextConfiguration 指定 spring 配置文件的位置

@ContextConfiguration(locations= {"classpath:applicationContest.xml"})

@ContextConfiguration 註解:

  • locations 屬性:用於指定配置文件的位置。如果是類路徑下,需要用 classpath:表明
  • classes 屬性:用於指定註解的類。當不使用 xml 配置時,需要用此屬性指定註解類的位置。

第四步:使用@Autowired 給測試類中的變量注入數據

    @Autowired
    private IAccountService ac;

在這裏插入圖片描述

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