Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to make groovy Syntax in spock

Shulou Source: shulou.com Published: 2022-06-02 10:52:13 10月04日 Update

This article is to share with you about how to make groovy grammar in spock. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Introduction

When using the spock test framework, the following test code often appears:

/ / Test method def "test plus 1" () {given: "prepare data" def a = 1 def b = 2 when: "Test method" def c = calculateService.plus (a, b) then: "check result" c = = 4-1}

Many people treat it as a fixed format, especially those like me who started groovy and spock from java in a few days, and hardly delve into what grammar this is.

In fact, some groovy-specific syntax formats are used in the spock framework, which are not common in Java, as follows:

Labeled statements label statement

Any statement can be associated with a label. Labels do not impact the semantics of the code and can be used to make the code easier to read like in the following example:

Any statement can be associated with a tag, which not only does not change the semantics, but also makes the statement more readable, as follows:

Given: def x = 1 def y = 2when: def z = x+ythen: assert z = = 3

In fact, there is also a tag statement in java, with the following code:

For (int iTuno Bandi)

Tags: Statements tests syntax code tags frames results methods that is strength data more formats knowledge articles preparation complexity utility clarity advantages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Docker OPPO Reno Microsoft Linux