最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java Config下的Spring Test幾種方式實(shí)例詳解

 更新時(shí)間:2017年05月02日 11:57:48   投稿:lqh  
這篇文章主要介紹了Java Config下的Spring Test方式實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下

Java Config 下的Spring Test方式

用了三種方式:

1.純手動(dòng)取bean:

package com.wang.test;

import com.marsmother.commission.core.config.MapperConfig;
import com.marsmother.commission.core.config.PropertyConfig;
import com.marsmother.commission.core.config.ServiceConfig;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.service.UserService;
import com.marsmother.commission.site.config.SecurityConfig;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by Wanglei on 15/10/29.
 */
public class CustomeTest {

  private static AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

  @Before
  public void tearUp(){
    context.register(PropertyConfig.class);
    context.register(ServiceConfig.class);
    context.register(SecurityConfig.class);
    context.register(MapperConfig.class);
    context.refresh();
  }

  @Test
  public void testUser(){
    UserService userService = context.getBean(UserService.class);
    Long userId = 3L;
    GeneralResponseData data = userService.addUserRelation(userId);
    System.out.println(data.getMsg());
  }
}

2.采用spring-test

package com.wang.test;

import com.marsmother.commission.core.config.MapperConfig;
import com.marsmother.commission.core.config.PropertyConfig;
import com.marsmother.commission.core.config.ServiceConfig;
import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.service.UserService;
import com.marsmother.commission.site.config.SecurityConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by Wanglei on 15/10/29.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {PropertyConfig.class, ServiceConfig.class, SecurityConfig.class, MapperConfig.class})
public class SpringTest {

  @Autowired
  private UserService userService;

  @Test
  public void testUser(){
    GeneralResponseData data= userService.addUserRelation(3L);
    System.out.println(data.getMsg());
  }

}

3.采用Mockito

需要引入相應(yīng)包:

<dependency>
  <groupId>org.mockito</groupId>
  <artifactId>mockito-all</artifactId>
  <version>1.9.5</version>
  <scope>test</scope>
</dependency>
package com.wang.test;

import com.marsmother.commission.core.dto.GeneralResponseData;
import com.marsmother.commission.core.presistence.FollowNumberMapper;
import com.marsmother.commission.core.presistence.UserMapper;
import com.marsmother.commission.core.presistence.UserRelationMapper;
import com.marsmother.commission.core.service.UserService;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/**
 * Created by Wanglei on 15/10/29.
 */
public class TestUserService {

  @InjectMocks
  private UserService userService;

  @Mock
  private FollowNumberMapper followNumberMapper;
  @Mock
  private UserMapper userMapper;
  @Mock
  private UserRelationMapper userRelationMapper;

  @Before
  public void init(){
    MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testUser(){
    Long userId = 3L;
    GeneralResponseData result = userService.addUserRelation(userId);
    System.out.println(result.getMsg());
  }

}

這里@Mock的話,并不會(huì)真正的去執(zhí)行數(shù)據(jù)庫(kù)的操作。

還有一種用法是@Spy,暫時(shí)不了解具體使用方式,待研究。

相比之下,還是spring-test標(biāo)準(zhǔn)一些。

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • 淺析Java中關(guān)鍵詞volatile底層的實(shí)現(xiàn)原理

    淺析Java中關(guān)鍵詞volatile底層的實(shí)現(xiàn)原理

    在 Java 并發(fā)編程中,有 3 個(gè)最常用的關(guān)鍵字:synchronized、ReentrantLock 和 volatile,這篇文章主要來(lái)和大家聊聊volatile底層的實(shí)現(xiàn)原理,感興趣的可以了解下
    2024-02-02
  • Java實(shí)現(xiàn)斗地主最簡(jiǎn)代碼實(shí)例

    Java實(shí)現(xiàn)斗地主最簡(jiǎn)代碼實(shí)例

    在本篇文章里小編給各位分享的是關(guān)于Java實(shí)現(xiàn)斗地主最簡(jiǎn)代碼實(shí)例,有興趣的朋友們可以參考下。
    2020-05-05
  • java實(shí)現(xiàn)馬踏棋盤(pán)的完整版

    java實(shí)現(xiàn)馬踏棋盤(pán)的完整版

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤(pán)的完整版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Elasticsearch模糊查詢?cè)敿?xì)介紹

    Elasticsearch模糊查詢?cè)敿?xì)介紹

    這篇文章主要給大家介紹了關(guān)于Elasticsearch模糊查詢的相關(guān)資料,在數(shù)據(jù)庫(kù)查詢中模糊查詢是一種強(qiáng)大的技術(shù),可以用來(lái)搜索與指定模式匹配的數(shù)據(jù),需要的朋友可以參考下
    2023-09-09
  • 使用Vert.x Maven插件快速創(chuàng)建項(xiàng)目的方法

    使用Vert.x Maven插件快速創(chuàng)建項(xiàng)目的方法

    這篇文章主要介紹了使用Vert.x Maven插件快速創(chuàng)建項(xiàng)目的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Java StringBuilder類(lèi)原理及常用方法

    Java StringBuilder類(lèi)原理及常用方法

    這篇文章主要介紹了Java StringBuilder類(lèi)原理及常用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式

    如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式

    這篇文章主要介紹了如何使用SpringMVC的消息轉(zhuǎn)換器設(shè)置日期格式問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • SpringCloud Gateway的路由,過(guò)濾器和限流解讀

    SpringCloud Gateway的路由,過(guò)濾器和限流解讀

    這篇文章主要介紹了SpringCloud Gateway的路由,過(guò)濾器和限流解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例

    java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求方法示例

    這篇文章主要給大家介紹了關(guān)于java使用common-httpclient包實(shí)現(xiàn)post請(qǐng)求的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-08-08
  • Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例

    這篇文章主要介紹了Spring MVC實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)增刪改查完整實(shí)例,從創(chuàng)建一個(gè)web項(xiàng)目開(kāi)始,分享了項(xiàng)目結(jié)構(gòu)以及具體Java代碼和前端頁(yè)面等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以了解下。
    2017-12-12

最新評(píng)論

北宁市| 礼泉县| 江达县| 包头市| 晴隆县| 九江县| 都江堰市| 嫩江县| 团风县| 安陆市| 灵宝市| 玉田县| 塔河县| 右玉县| 酒泉市| 峨边| 林周县| 绿春县| 广德县| 阜宁县| 兴隆县| 慈利县| 高雄市| 嵊泗县| 南通市| 隆昌县| 章丘市| 庄浪县| 凌云县| 建阳市| 西充县| 山东| 松潘县| 黎平县| 台山市| 礼泉县| 崇左市| 安乡县| 宁德市| 瑞昌市| 廊坊市|