gpt4 book ai didi

java - 如何使用 Spock 测试单个类的 Logger?

转载 作者:太空宇宙 更新时间:2023-11-04 06:42:03 25 4
gpt4 key购买 nike

所以我有一节课:

ExtractJustAddedObjectTypeIDsBolt {
private static Logger logger = LoggerFactory.getLogger(ExtractJustAddedObjectTypeIDsBolt.class);

public void someMethod() {
logger.error("this is an error");
}
}

ClassOneTest extends Specification {
ExtractJustAddedObjectTypeIDsBolt bolt;

void setup() {
this.bolt = new ExtractJustAddedObjectTypeIDsBolt();
}

void cleanup() { // run after every feature method

}

def "test someMethod"() {
given:

when:
bolt.someMethod();

then:
bolt.logger.error("this is an error");
}
}

但这就是我得到的错误:

199  [main] ERROR member.bolt.ExtractJustAddedObjectTypeIDsBolt - this is an error

Too few invocations for:

1 * bolt.logger.error("this is an error") (0 invocations)

Unmatched invocations (ordered by similarity):

最佳答案

尝试:

   def "test someMethod"() {
given:
def logger = Mock(Logger)
and:
bolt.logger = logger
when:
bolt.someMethod()
then:
1 * logger.error("this is an error")
}

您需要首先模拟 Logger。

关于java - 如何使用 Spock 测试单个类的 Logger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24560252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com