java motherfree video_Java Config 下的Spring Test方式

java motherfree video_Java Config 下的Spring Test方式用了三种方式:1.纯手动取bean:packagecom.wang.test;importcom.marsmother.commission.core.config.MapperConfig;importcom.marsmother.commission.core.config.PropertyConfig;importcom.marsmother.commission.core.conf…

大家好,又见面了,我是你们的朋友全栈君。

用了三种方式:

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

需要引入相应包:

org.mockito

mockito-all

1.9.5

test

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/156603.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月7日 下午6:36
下一篇 2022年7月7日 下午6:36


相关推荐

  • 【MyBatis】MyBatis分页插件PageHelper的使用

    【MyBatis】MyBatis分页插件PageHelper的使用好多天没写博客了 因为最近在实习 大部分时间在熟悉实习相关的东西 也没有怎么学习新的东西 这周末学习了 MyBatis 的一个分页插件 PageHelper 虽然没有那么的强大 我在最后会说明它的缺点 但还是挺不错的 这篇博文主要来总结下如何使用 PageHelper 我们知道 在 mysql 中 分页的 sql 是使用 limit 来做 如果我们自己写 sql 那分页肯定是没有任何问题的 但是一旦 model 多

    2026年3月26日
    2
  • git使用giee

    git使用giee1 在当前桌面新建一个文件夹右键 git 打开 gitclonegit 的地址 gitclonehttp gitee com heibaizi2000 gitstudy git2 将修改后的项目或者添加的项目或者文件放到这里面 3 gitadd 添加到缓冲区 4 gitcommit m 备注 提交 5 gitpush 上传

    2026年3月19日
    1
  • 5G nr频段_5g哪个信道信号强

    5G nr频段_5g哪个信道信号强文章目录1.工作频段2.基站信道带宽2.1传输带宽配置2.2最小保护带3.信道安排3.1信道栅格3.2同步栅格参考文献1.工作频段NR工作在两大频率范围(FrequencyRange,FR):FR1和FR2,如下表1-1所示[1]。表1-1.频率范围的定义[1](TS38.104Table5.1-1)FR1和FR2中,又划分了多个不同的工作频段,如下表1-2和下表1-3所示[1]。表中的n代表NR。表1-2.NR在FR1中的工作频段[1](TS38

    2022年10月6日
    6
  • busybox 安装mysql_busybox怎么安装

    busybox 安装mysql_busybox怎么安装把”busybox-armv6l”重命名为”busybox”;将busybox传入手机的SD卡,打开terminal(Linux,Mac)或cmd(Windows)adbpush~/Desktop/busybox/mnt/sdcard其中的~/Desktop请根据自己的情况替换成正确的路径输入以下命令,为了在/system目录写入文件adbshellsumount-oremount,r…

    2022年7月25日
    18
  • Hunyuan-MT-7B保姆级教程:从部署到调用全流程解析

    Hunyuan-MT-7B保姆级教程:从部署到调用全流程解析

    2026年3月12日
    1
  • vim 支持python_如何进入python

    vim 支持python_如何进入pythonLinux 下的 python 虚拟环境 + vim快捷方式

    2022年4月22日
    61

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号