Java程序中对Service进行Mock

2023-11-10

Java程序中对Service进行Mock

背景

在项目中往往需要对service逻辑进行单元测试验证,这里采用mockito对dao数据进行模拟,验证service逻辑

Servie

package com.xx.kdc.service;

import com.xx.kdc.dao.codegen.TestMapper;
import com.xx.kdc.model.codegen.Test;
import com.xx.kdc.model.codegen.TestExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TestService {

    @Autowired
    private TestMapper testMapper;

    public Long save(Test test){
         testMapper.insert(test);
        return test.getId();
    }

    public void delete(Long id){
        testMapper.deleteByPrimaryKey(id);
    }

    public Test getById(Long id){
        return testMapper.selectByPrimaryKey(id);
    }

    public List<Test> getByName(String name){
        TestExample testExample = new TestExample();
        testExample.createCriteria().andTestNameEqualTo(name);

        return testMapper.selectByExample(testExample);
    }

    public void update(Test test){
        testMapper.updateByPrimaryKey(test);
    }


}

Test Service

TestServiceTest.java

package com.xx.kdc.service;

import com.xx.kdc.dao.codegen.TestMapper;
import com.xx.kdc.model.codegen.TestExample;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.omg.CORBA.PUBLIC_MEMBER;

import javax.inject.Inject;

import java.util.Arrays;
import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
 * @author cao.shengyong
 * @date 2021/2/24 14:26
 */
public class TestServiceTest {

    @InjectMocks
    private TestService testService;

    @Mock
    private TestMapper testMapper;

    @Before
    public void before() {
        // 注入依赖
        MockitoAnnotations.openMocks(this);
    }

    @Test
    public void test_getById(){

        com.westone.chengxun.kdc.model.codegen.Test test = new com.westone.chengxun.kdc.model.codegen.Test();
        test.setTestName("asd");
        test.setId(1L);

        when(testMapper.selectByPrimaryKey(1L)).thenReturn(test);

        com.westone.chengxun.kdc.model.codegen.Test byId = testService.getById(1L);

        Assert.assertTrue(byId != null);
    }

    @Test
    public void test_getByName(){
        String testName = "aaa";
        com.westone.chengxun.kdc.model.codegen.Test test = new com.westone.chengxun.kdc.model.codegen.Test();
        test.setTestName(testName);
        test.setId(1L);


        when(testMapper.selectByExample(any(TestExample.class))).thenReturn(Arrays.asList(test));

        List<com.westone.chengxun.kdc.model.codegen.Test> list = testService.getByName(testName);

        Assert.assertTrue(list.size() == 1);
    }
    @Test
    public void test_save(){
        com.westone.chengxun.kdc.model.codegen.Test test = new com.westone.chengxun.kdc.model.codegen.Test();
        test.setTestName("bb");
        test.setId(1L);
        Long id = testService.save(test);

        Assert.assertTrue(id == 1L);
    }

    @Test
    public void test_update(){
        com.westone.chengxun.kdc.model.codegen.Test test = new com.westone.chengxun.kdc.model.codegen.Test();
        test.setTestName("cc");
        testService.update(test);
    }

    @Test
    public void test_delete(){
        testService.delete(1L);
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java程序中对Service进行Mock 的相关文章

  • 初学shell脚本之-bash: /home/test/hello.sh: Permission denied

    今天学习shell脚本在windows写了个shell脚本 span class token operator span span class token operator span bin span class token operato
  • test

    1 overrides the s4 notdlg class items as display none lt script type 61 34 text javascript 34 gt var fV4UI 61 true lt sc
  • Java线程test

    1 银行有一个账户有两个储户存分别向同一个账户存3000 元 xff0c 每次存1000 xff0c 存3次 每次存完打印账户余额 问题 xff1a 该程序是否有安全问题 xff0c 如果有 xff0c 如何解决 xff1f 提示 1 xf
  • Permutation Test

    常规步骤 xff1a 一般 多组间样本均数的比较是先做正态性检验和方差齐性 xff0c 然后在各组 都满足 正态性和方差齐性的前提下做方差分析 如果数据呈现正态分布 xff0c 但方差不齐 xff0c 则可以选择近似方法 Welch法 和
  • name ‘train_test_split‘ is not defined解决方法

    name 39 train test split 39 is not defined解决方法 解决方式 xff0c 用model selection模块导入 xff1a span class token keyword from span
  • 打印 Go Test 的代码覆盖

    打印 Go Test 的代码覆盖 本文地址 xff1a blog lucien ink archives 520 使用方法 将这段代码复制进 zshrc 或者是 bashrc 等文件中 xff08 取决于你的命令行 xff09 xff0c
  • Permutation test

    Outlines Goals and strategyThe simple conditionThe complex condition Goals and strategy Goals Estimate the distribution
  • 1:Unit test and main function-Java API 实战

    目录 1 抛出企业问题 xff0c 脱离main测试 xff0c 模块化编程2 Junit单元测试的含义和用途3 怎么获取各种Jar包 xff1f Maven Repository 获取各类各个版本的jar xff0c 这就是仓库 脱离老师
  • Using lxd to do vlan test (by quqi99)

    作者 xff1a 张华 发表于 xff1a 2022 08 15 版权声明 xff1a 可以任意转载 xff0c 转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 问题 客户说sriov虚机里收不着arp reply 他们的s
  • my cloud test bed (by quqi99)

    作者 张华 发表于 2023 03 10 版权声明 可以任意转载 转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明 问题 有一台NUC minipc 配置是 CPU i7 13700H 16核20线程 内存 16G 32G 4
  • Test Case Framework (TCF) tcf 参数

    root 64 embargo ww13 tcf help usage tcf h v log pid tid log time config file CONFIG FILE py p CONFIG PATH state path STA
  • train,val,test的区别

    参考 xff1a https www mobibrw com 2017 7966
  • INVALID TASK ‘.TEST.SKIP=TRUE’: YOU MUST SPECIFY A VALID LIFECYCLE PHASE […]

    问题 当我们在用如下命令行进行编译时 mvn Dmavne test skip span class token operator 61 span ture clean span class token function install s
  • px4混控器流程理解 motor test qgc调试

    sys autostart 索引是5000 启动是加载5000 generic ground vehicle文件 xff0c 此脚本指定混控脚本文件55行是rover generic 华丽分界线 四悬翼 sys autostart 索引是4
  • NS3 的 ipv4-static-routing-test-suite 源码分析

    下面进行源码注释 xff1a End to end tests for Ipv4 static routing include 34 ns3 boolean h 34 include 34 ns3 config h 34 include 3
  • 在 Bash 中检查文件是否为空的 5 种方法

    在 Linux 中 空文件是大小为零字节的文件 这意味着该文件不包含任何数据 并且在文本编辑器中打开时没有任何内容 可以使用 touch 命令创建一个空文件 touch myfile txt 这将在当前目录中创建一个名为 myfile tx
  • shell test功能

    test测试功能 对于要测试系统上面某些文件或其相关属性时 可以使用test进行测试 test会根据相关功能返回True或False 测试文件类型test e filename 测试功能 意义 e 该文件是否存在 f 该文件名是否存在且为文
  • 坚持天天写技术笔记

    恍恍惚惚
  • Selenium脚本转化Java代码

    Selenium录制的脚本可以转换Java JUnit单元测试 可以使用Java项目管理工具Maven统一运行 从Selenium脚本中导出Java代码 按照如下步骤 使用Java运行Selenium脚本 使用Chome导出Java脚本必须
  • 嵌入式经典面试题

    文章目录 一 常见面试题 1 用预处理指令 define 声明一个常数 用以表明1年中有多少秒 忽略闰年问题 2 写一个 标准 宏MIN 这个宏输入两个参数并返回较小的一个 3 预处理器标识 error的目的是什么 4 数据声明 5 sta

随机推荐