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


相关推荐

  • Spring Aop底层原理详解(利用spring后置处理器实现AOP)

    Spring Aop底层原理详解(利用spring后置处理器实现AOP)写在前面:对于一个java程序员来说,相信绝大多数都有这样的面试经历,面试官问:你知道什么是aop吗?谈谈你是怎么理解aop的?等等诸如此类关于aop的问题。当然对于一些小白可能会一脸懵逼;对于一些工作一两年的,可能知道,哦!aop就是面向切面变成,打印日志啊,什么什么的,要是有点学习深度的呢可能会说aop底层实现利用了jdk动态代理,cglib啊什么的。很多时候可能面试就到此打住了,当然,然后也…

    2025年12月5日
    4
  • jediscluster.set_java.lang.throwable

    jediscluster.set_java.lang.throwable如果使用的是redis3.x中的集群,在项目中使用jedisCluster。1、项目结构2、pom.xml123xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd”>454.0.067com.xxx8myboot9…

    2022年10月14日
    4
  • 动态规划优缺点_巴西优化航空路线利用率

    动态规划优缺点_巴西优化航空路线利用率C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市。任意两个城市之间最多只有一条道路直接相连。这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道路在统计条数时也计为 1 条。C 国幅员辽阔,各地的资源分布情况各不相同,这就导致了同一种商品在不同城市的价格不一定相同。但是,同一种商品在同一个城市的买入价和卖出价始终是相同的。商人阿龙来到 C 国旅游。当他得知“同一种商品在不同城市的价格可能会不同”这一信息之后,便决定在旅游的同时,利用商品在

    2022年8月9日
    5
  • 实战教程!告别昂贵的Claude,开箱即用,OpenClaw中文版Agent平台,原生支持DeepSeek、Qwen国产大模型

    实战教程!告别昂贵的Claude,开箱即用,OpenClaw中文版Agent平台,原生支持DeepSeek、Qwen国产大模型

    2026年3月13日
    2
  • Java后端实现-元素拖动排序

    Java后端实现-元素拖动排序ApiOperation value 板块拖动排序 ApiImplicitP ApiImplicitP name plateId1 value 拖拽体 ApiImplicitP name plateId2 value 参照体 ApiImplicitP name type value 实现类型 1 交换位置 2 挤压位置

    2026年3月17日
    2
  • linux安装busybox教程

    linux安装busybox教程获取软件包下载busybox安装包1.31.0当前最新稳定版本,并解压:wgethttps://qqq-1253133144.cos.ap-chengdu.myqcloud.com/bbb/busybox-1.31.0.tar.bz2tar-xjvfbusybox-1.31.0.tar.bz2编译环境配置l编译和安装busybox:cdbusybox-1.31.0/makedefconfig&&makeinstallbusyb.

    2022年7月15日
    24

发表回复

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

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