IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 使用groovy 进行java单元测试-基础使用 -> 正文阅读

[开发测试]使用groovy 进行java单元测试-基础使用

参考:https://semaphoreci.com/community/tutorials/stubbing-and-mocking-in-java-with-the-spock-testing-framework

了解groovy

Groovy 是一种基于 JVM 的敏捷开发语言,它结合了 Python、Ruby 和 Smalltalk 的许多强大的特性,Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码。由于其运行在 JVM 上的特性,Groovy 可以使用其他 Java 语言编写的库。

另请注意,任何语句的行尾都没有分号。与 Java 不同,Groovy 不需要分号。

1:引入的包

grandle方式

//1 使用插件,引用 groovy 要不会 不识别的
plugins {
    id 'io.jmix' version '1.0.1'
    id 'java'
    id 'groovy'
}
//2 引用spck包
testImplementation "org.spockframework:spock-core"
testImplementation "org.spockframework:spock-spring"

2:了解语法

given: “a customer with example name values”
and: “an entity manager that always returns this customer”
and: “a customer reader which is the class under test”
when: “we ask for the full name of the customer”
then: “we get both the first and the last name”

3:新建groovy文件

在这里插入图片描述

4:service方法

这里 一定要在service 里面 用构造注入的方式,一定不要用@Autowried 方式,否则groovy测试的时候找不到的

@Service(RemoteAgentService.NAME)
public class RemoteAgentServiceBean implements RemoteAgentService {


    private final AgentService agentService;
	//就是这里,构造注入
    public RemoteAgentServiceBean(AgentService agentService) {
        this.agentService = agentService;
    }

    @Override
    @Authenticated
    public Agent register(String name, UUID agentId, String host, String version, String ipAddress) {

        if(!validateParams(name,agentId,host,version,ipAddress)){
            return null;
        }else{
            //T-O-D-O 校验
            return Optional.ofNullable(agentService.getAgentByExternalId(agentId, AGENT_CHECK_VIEW))
                    .orElseGet(() -> agentService.createAgent(name, agentId, host, version, ipAddress));
        }
    }
}

具体测试类

方式1:

class RemoteAgentServiceTest extends Specification {


    def "Test"() {
        def now = "1111111231231"
        println now
        expect:
        assertThat(now).isNotNull()
        assertThat(now).isEqualTo("1111111231231")
    }
	//就是这里这样注入service 就可以了	
    AgentService agentService
    def serviceBean
    void setup(){
    	//这里AgentService表示需要mock出来的测试类
        agentService= Mock(AgentService.class)
        //按照service方法中注入的方式 引入其他service将自己 注入进去
        serviceBean = new RemoteAgentServiceBean(agentService)
    }

    /**
     * 注册 单元测试
     * @return
     */
    def "register"() {
        //入参
        given:
        def name = "test2"
        def agentId = UUID.fromString("1235922b-f697-47db-b237-2078b090c43a")
        def host = ""
        def version = "1.0.0"
        def ipAddress = "172.17.0.12"
        def view = "agent-check-view"

        and:
        //这里的 >> 就表示mock 返回的值
        agentService.getAgentByExternalId(agentId, view) >> null

        when:
        //这里的 >> 就表示mock 返回的值
        agentService.createAgent(name, agentId, host, version, ipAddress) >> new Agent(name:name)
        def agent = serviceBean.register(name, agentId, host, version, ipAddress)

        then:
        agent.name=="test2"
    }


}

方式2:

class RemoteAgentServiceTest extends Specification {

    AgentService agentService
    RemoteAgentServiceBean serviceBean

    static UUID randomUUID = UUID.randomUUID()
    static UUID uuid = UUID.fromString("1235922b-f697-47db-b237-2078b090c43a")
    static String view = "agent-check-view"

    @SuppressWarnings(['GrUnresolvedAccess', 'unused'])
    def setup() {
        agentService = Stub {
        	//同时构造两种情况  >>
            getAgentByExternalId(uuid, view) >> new Agent(name: "registered agent")
            getAgentByExternalId(randomUUID, view) >> null
        }
        serviceBean = new RemoteAgentServiceBean(agentService)
    }

    /**
     * 注册 单元测试
     * @return
     */
    def "register"(String name, UUID externalId) {

        given: "定义入参"
        def host = ""
        def version = "1.0.0"
        def ipAddress = "172.17.0.12"

        and: "模拟创建agent方法"
        agentService.createAgent(name, externalId, host, version, ipAddress) >> new Agent(name: name)

        expect: "注册agent"
        def agent = serviceBean.register(name, externalId, host, version, ipAddress)
        result == agent.getName()

        where: "测试"
        // name + externalId =result ,也就是有两种情况,两种结果
        name    | externalId || result
        "test1" | uuid       || "registered agent"
        "test2" | randomUUID || "test2"
    }

}
  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章      下一篇文章      查看所有文章
加:2021-09-19 08:16:52  更:2021-09-19 08:17:00 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 20:58:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码