Spock存根未返回期望值

阿什莉·理查德·沃克

我正在尝试使用Spock存根在服务类中模拟数据库/存储库的依赖关系,但是存根返回意外值时遇到问题。我不明白为什么只有在不将参数传递给模拟方法时存根才起作用。

given: 'I have client data'
Client client = new Client("Foo", "[email protected]")

and: 'a client repository always returns the id'
clientRepository = Stub(ClientRepository)
ClientEntity ce = new ClientEntity("Foo", "[email protected]")
clientRepository.create(ce) >> 1

when: 'I add the client'
ClientService clientService = new ClientServiceImpl(clientRepository)
Client addedClient = clientService.addClient(client)

then: 'The client object should be populated correctly'
addedClient.getId() == 1 // This fails b/c it's returning the id as 0

但是当我使用_参数测试通过时:

given: 'I have client data'
Client client = new Client("Foo", "[email protected]")

and: 'a client repository always returns the id'
clientRepository = Stub(ClientRepository)
clientRepository.create(_) >> 1

when: 'I add the client'
ClientService clientService = new ClientServiceImpl(clientRepository)
Client addedClient = clientService.addClient(client)

then: 'The client object should be populated correctly'
addedClient.getId() == 1 // This passes b/c it's returning the id as 1

这是服务类

@Service
public class ClientServiceImpl implements ClientService{

    private ClientRepository clientRepository;

    @Autowired
    ClientServiceImpl(ClientRepository clientRepository){
        this.clientRepository = clientRepository;
    }

    @Override
    public Client addClient(Client client){

        ClientEntity clientEntity = new ClientEntity(
                client.getName(),
                client.getEmailAddress()
        );

        int id = clientRepository.create(clientEntity);
        client.setId(id);

        return client;
    }
}

和Spock的依赖

 <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.3-groovy-2.5</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-spring</artifactId>
            <version>1.3-groovy-2.5</version>
            <scope>test</scope>
        </dependency>

谢谢您的帮助!

克里加克斯

如果你这样做

ClientEntity ce = new ClientEntity("Foo", "[email protected]")
clientRepository.create(ce) >> 1

并且未执行存根交互,则因为方法参数未根据您的期望进行匹配。我的猜测是,equals(..)方法ClientEntity不能按您期望的那样工作,并且给定的参数create(..)不完全ce是它的副本,而不能满足要求equals(..)

解决方案:修正您的equals(..)方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

COUNTIF公式未返回期望值

来自分类Dev

Ruby命令未返回期望值

来自分类Dev

BigInteger数学函数未返回期望值

来自分类Dev

date.getTimezoneOffset()未返回期望值

来自分类Dev

Mock上的设置未返回期望值

来自分类Dev

正则表达式未返回期望值

来自分类Dev

math.h中的函数未返回期望值

来自分类Dev

日期时间转换未返回期望值

来自分类Dev

Hibernate搜索通配符未返回期望值

来自分类Dev

字符串数组未返回期望值

来自分类Dev

DATEDIF()未返回期望值,如何计算月龄?

来自分类Dev

c的除法未给出期望值

来自分类Dev

awk OFS未产生期望值

来自分类Dev

任务ContinueWith不返回期望值

来自分类Dev

标量UDF不返回期望值

来自分类Dev

阿拉伯文化日期时间解析未返回期望值

来自分类Dev

具有日期条件的SQL窗口函数未返回期望值

来自分类Dev

Redshift中的REGEXP_SUBSTR函数未返回期望值

来自分类Dev

汇总列中的总值时,SQL查询未返回期望值

来自分类Dev

在一种特定情况下,函数未返回期望值

来自分类Dev

阿拉伯文化日期时间解析未返回期望值

来自分类Dev

字符串枚举未打印期望值

来自分类Dev

Verilog在简单分配中未输出期望值

来自分类Dev

Mockito什么时候/然后不返回期望值

来自分类Dev

preg_match_all不返回期望值

来自分类Dev

嵌套匹配的索引不返回期望值

来自分类Dev

XNA Rectangle.Center不返回期望值

来自分类Dev

递归函数未在PHP中返回期望值

来自分类Dev

preg_match_all不返回期望值