JavaConfig下的SpringTest几种方式实例详解

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标准一些。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

新闻标题:JavaConfig下的SpringTest几种方式实例详解
文章路径:https://www.cdcxhl.com/article32/jpoosc.html

成都网站建设公司_创新互联,为您提供全网营销推广网站营销品牌网站建设网站设计ChatGPT软件开发

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

营销型网站建设