小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring整合Junit

 Coder編程 2020-05-31

一. 導(dǎo)入相關(guān)依賴

(1) junit依賴

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

注:Spring 5.x 版本整合Junit時,Junit的版本要求必須在4.12及以上,否則會報錯

Spring整合Junit版本問題

(2)spring-test

    <!--Spring-test-->
    <!-- https:///artifact/org.springframework/spring-aspects -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.2.RELEASE</version>
    </dependency>

二. 添加注解

在測試類添加注解:

/**
	* RunWith注解:
	* 作用: 把原有的main方法替換成spring提供的main方法
	* 寫法固定
	*/
@RunWith(SpringJUnit4ClassRunner.class)
/**
   * ContextConfiguration注解:
   * locations屬性: 用于指定Spring配置文件的路徑
   * classes屬性: 用于指定注解類所在地的路徑
   */
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
// 注解類寫法:
//@ContextConfiguration(classes = SpringConfiguration.class)
public class MapperTest {
	//實現(xiàn)自動依賴注入
    @Autowired
    private DepartmentMapper departmentMapper;

    @Test
    public void testCRUD(){
        //使用依賴注入的對象
        System.out.println(departmentMapper);
    }
}

三.示例代碼

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class MapperTest {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Test
    public void testCRUD(){
        System.out.println(departmentMapper);
    }
}

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多