Spock Mock with Guava Collection

时间:2015-10-05 15:58:19

标签: java unit-testing mocking guava spock

我在尝试模拟Guava Collection中的依赖时遇到了困难。

我们假设我有以下代码进行测试:

@Service
public final class ServiceA {

    private final ServiceB serviceB;

    @Autowired
    public ServiceA(ServiceB serviceB) {
        this.serviceB = serviceB;
    }

    public Collection<String> runAll(Collection<String> dataList) {
        final ImmutableList.Builder<String> builder = ImmutableList.builder();

        for (String data : dataList) {
            builder.add(serviceB.run(data));
        }

        return builder.build();
    }
}

My Spock Spec看起来像这样:

class ServiceASpec extends Specification {
    def serviceB = Mock(ServiceB.class)
    def serviceA = new ServiceA(serviceB)

    def "test"() {
        when:
        def strings = serviceA.runAll(['data1', 'data2'])

        then:
        1 * serviceB.run('data1') >> 'result1'
        1 * serviceB.run('data2') >> 'result2'
        0 * _._

        strings == ['result1', 'result2']
    }
}

这个规范运行得很好,它正在做我想做的事。

然后我重构了我的实现以使用Guava的Collections2.transform(..): -

@Service
public final class ServiceA {

    private final ServiceB serviceB;

    @Autowired
    public ServiceA(ServiceB serviceB) {
        this.serviceB = serviceB;
    }

    public Collection<String> runAll(Collection<String> dataList) {
        return Collections2.transform(dataList, new Function<String, String>() {
            @Override
            public String apply(final String input) {
                return serviceB.run(input);
            }
        });
    }
}

当我重新运行我的规范时,我收到此错误: -

Too few invocations for:

1 * serviceB.run('data1') >> 'result1'   (0 invocations)

Unmatched invocations (ordered by similarity):

None

Too few invocations for:

1 * serviceB.run('data2') >> 'result2'   (0 invocations)

Unmatched invocations (ordered by similarity):

None

我认为它与模拟时间有关,因为只有在使用集合时才会执行Guava函数。

但是,我不确定如何重构我的规范以使其发挥作用。

我该如何解决这个问题?感谢。

1 个答案:

答案 0 :(得分:2)

引擎盖下transform()方法返回TransformedCollection类。正如您所见,here转换仅在迭代包装集合时应用。由于您没有迭代转换后的集合,因此不会调用模拟服务,也不会记录任何交互。

似乎简单地迭代集合应该可以解决问题,但是这样的测试应该有很好的文档记录。

另一种方法是使用FluentIterable.from(list).transform(function).toList()代替Collections2.transform(list, function)