在 spock 中交换数字测试用例

Swap number test case in spock

我只是想尝试使用 Spock 测试框架,根据文档,它看起来比 JUnit 更好,并尝试编写简单的测试用例,但不知何故它不起作用。

Class:

public class SwapTwoNumber {
    public void swapNumber(int a,int b){
        a =a+b;
        b=a-b;
        a=a-b;

    }
}

Spock 测试 Groovy:

import spock.lang.Specification

class SwapTwoNumberTest extends Specification{
    def "swap to number as given. #a and #b"() {
     given:
      int a = 5
      int b = 6
     when:
      SwapTwoNumber sw =Mock()
      sw.swapNumber(a,b)
      then:
      a==6
      b==5

    }
}

我不熟悉你提到的测试框架,但这很可能不是框架问题。您正在传递原语参数。

docs

中所述

Primitive arguments, such as an int or a double, are passed into methods by value. This means that any changes to the values of the parameters exist only within the scope of the method. When the method returns, the parameters are gone and any changes to them are lost.