gpt4 book ai didi

java - Spock 测试因 MissingPropertyException 失败

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

我有一个简单的类,它根据简单的规则构建 RequestMessage

  • 如果在数据库中找到MessageMetadata,则会使用该元数据构建消息。
  • 否则,消息将使用提供元数据的服务构建。

这是该类以及 Spock 测试。测试失败并出现 MissingPropertyException,表示找不到使用该服务创建的 RequestMessage

class MessageService{
MessageBuilder messageBuilder;
MessageRepository messageRepository;
MessageMetadaProvider messageMetaDataProvider;

public RequestMessage getMessage(String id ) {
try {
MessageMetadata metadata = repository.findDefaulMessage(id);
RequestMessage requestMessage = builder.createMessage(metadata);
return requestMessage;
} catch (DataNotFoundException e) {
e.printStackTrace();
}
MessageMetadata metadata = messageMetaDataProvider.createNewMessageMetadata(id);
RequestMessage message = messageBuilder.buildNew(id , metadata);
return message;
}
}

def "Build Request Message  "() {
given:
String id = '12345'
MessageBuilder messageBuilder = Mock()
MessageRepository messageRepository = Mock()
MessageMetadaProvider messageMetaDataProvider = Mock()
MessageService service = createMessageService(messageRepository,messageBuilder,messageMetaDataProvider)
MessageMetadata metadata = new MessageMedata(id, MessageType.DEFAULT)
when:
RequestMessage requestMessage = messageService .getMessage(id )
then:
1 * messageRepository.repository.findDefaulMessage(id) >> {throw new DataNotFoundException("Not Found")}
1 * messageMetaDataProvider.createNewMessageMetadata(id) >> metadata
1 * messageBuilder.buildNew(id , metadata) >> requestMessage
}

我真的很想了解为什么这不起作用。我很感谢您为成功测试此测试提供的任何帮助。谢谢

最佳答案

这个问题确实是我在对 jaco0646 的回答的评论中提到的母鸡与鸡蛋的问题。让我在 MCVE 中重现核心问题:

package de.scrum_master.stackoverflow.q60143388;

public class RequestMessage {}
package de.scrum_master.stackoverflow.q60143388;

public class MessageService {
MessageBuilder messageBuilder;

public RequestMessage getMessage(String id) {
return messageBuilder.buildNew(id);
}
}
package de.scrum_master.stackoverflow.q60143388

import spock.lang.Specification

class MessageServiceTest extends Specification {
def "build request message"() {
given:
def id = "12345"
MessageBuilder messageBuilder = Mock()
MessageService messageService = new MessageService()
messageService.messageBuilder = messageBuilder

when:
RequestMessage requestMessage = messageService.getMessage(id)

then:
1 * messageBuilder.buildNew(id) >> requestMessage
}
}

这将产生:

groovy.lang.MissingPropertyException: No such property: requestMessage for class: de.scrum_master.stackoverflow.q60143388.MessageServiceTest

at de.scrum_master.stackoverflow.q60143388.MessageServiceTest.build request message(MessageServiceTest.groovy:17)

仅按照 jaco0646 建议在 given: block 中声明 requestMessage,然后将其用作 stub 结果,这并不能单独完成。您需要确保首先声明一个由构建器方法返回的对象(例如模拟),但将测试下的方法调用的结果分配给另一个变量,以便您可以比较两者或执行您喜欢执行的任何验证:

package de.scrum_master.stackoverflow.q60143388

import spock.lang.Specification

class MessageServiceTest extends Specification {
def "build request message"() {
given:
def id = "12345"
RequestMessage requestMessage = Mock()
MessageBuilder messageBuilder = Mock()
MessageService messageService = new MessageService()
messageService.messageBuilder = messageBuilder

when:
RequestMessage result = messageService.getMessage(id)

then:
1 * messageBuilder.buildNew(id) >> requestMessage
result.toString().startsWith("Mock for type 'RequestMessage'")
}
}

现在测试通过了,您的引导问题也已解决。您不能通过方法调用创建对象,然后同时将其指定为预期的 stub 结果。这没有逻辑意义。

关于java - Spock 测试因 MissingPropertyException 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60143388/

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