首页 > 解决方案 > 在 Spock 中,如何测试从另一个静态方法调用的静态方法?

问题描述

这是我的课:

class Application {
    public static void main(){
        System.out.println("main called");
        otherMethod();
    }

    public static void otherMethod(){
        System.out.println("otherMethod called");
    }
}

这是我的 spock 测试

def "Expect that Application.otherMethod() is called"(){
    given:
    def app = new Application() 

    when:
    Application.main()

    then:
    1 * Application.otherMethod()
}

我怎样才能使这项工作?

标签: javagroovyspock

解决方案


你没有。静态方法不应该有可见的副作用(理想情况下,它们应该是纯函数),并且对于客户端如何实现它们并不重要。

在您的简单情况下,您将调用移动otherMethod()到构造函数中Applicationnew Application()main.


推荐阅读