Spock:如何让不在 class 下的静态方法测试 return 某个值?

Spock: How to let static method which is not in the class under test return certain value?

我正在使用带有 Groovy 的 Spock 来测试 class:

public class Animal {
    public void findAnimal() {
        findAnimalInZoo();     
    }

    private void findAnimalInZoo() {
        if (!isGoodWeather) {
            throw Exception;
        }
    }

    private boolean isGoodWeather() {
        return "good".equals(Weather.getWeather());
    }
}

Weather class:

public class Weather {
    public static String getWeather() {
        return instance.getWeather();
    }
}

现在在方法 findAnimal() 的每个测试用例中,我想指定调用 Weather.getWeather() 时返回的值。

def "when it is good weather then expect no exception"() {
    setup:
    // What should I do for Weather.getWeather()?    
}

我该怎么做?

如果您的生产代码是 Java,那么您不能使用 Spock 模拟来模拟静态方法。如果你的生产代码是Groovy,你可以使用这个:

GroovyMock(Weather, global: true)
Weather.getWeather() >> "good"

如果您受困于 Java,那么您需要使用 Powermock 或 JMockit 来实现。

def "when it is good weather then expect no exception"() {
    setup:
    PowerMockito.mockStatic(Weather)
    when(Account.getWeather()).thenReturn("good")
}

参考: https://dzone.com/articles/mocking-static-methods-groovy

迂腐警告 我知道您并不总是能够控制或改进您正在测试的代码,因此请注意接受此建议。

当您需要花费如此多的时间来测试您的代码时,您的代码就会对您大喊大叫,说它设计得很糟糕。您的代码与天气 class 紧密耦合。除非您通过重写 classes 对 VM 进行技巧处理,否则不可能将其切换为其他内容。这意味着不仅您的测试代码无法做到这一点,而且您的生产代码也不必要地不灵活。