91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Java Config下的Spring Test幾種方式實例詳解

發布時間:2020-10-01 14:11:22 來源:腳本之家 閱讀:174 作者:lqh 欄目:編程語言

Java Config 下的Spring Test方式

用了三種方式:

1.純手動取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

需要引入相應包:

<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的話,并不會真正的去執行數據庫的操作。

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

相比之下,還是spring-test標準一些。

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

黑水县| 西安市| 唐海县| 宁河县| 大洼县| 乌兰浩特市| 宝鸡市| 磐石市| 屏山县| 墨江| 罗定市| 文成县| 奇台县| 蚌埠市| 阆中市| 湘西| 徐州市| 应城市| 泸州市| 新平| 内丘县| 睢宁县| 巴林右旗| 仙游县| 嘉兴市| 永泰县| 双辽市| 石楼县| 额敏县| 云梦县| 阿鲁科尔沁旗| 治县。| 闽侯县| 南郑县| 望江县| 健康| 绥宁县| 静海县| 枣庄市| 祁连县| 兴化市|