小编Nad*_*dir的帖子

Spock mock在协作者中返回null,但在feature方法中没有

我有Spock Mock()对象的问题.我有一个我正在尝试测试的java类.这个类做了一些我想要模拟的ftp.我的示例代码

class ReceiveDataTest extends Specification{
    String downloadPath = 'downloadPath';
    String downloadRegex = 'downloadRegex';
    SftpUtils sftpUtils = Mock();
    ReceiveData receiveData;

    def setup(){
        sftpUtils.getFileNames(downloadPath,downloadRegex) >> ['file1', 'file2']
        receiveData= new ReceiveData()
        receiveData.setDownloadPath(downloadPath)
        receiveData.setDownloadRegex(downloadRegex)
        receiveData.setSftpUtils(sftpUtils);

    }

    def "test execute"() {
        given:
        def files = sftpUtils.getFileNames(downloadPath,downloadRegex)
        files.each{println it}
        when:
        receiveData.execute();
        then:
        1*sftpUtils.getFileNames(downloadPath,downloadRegex)
    }

}

public class ReceiveData(){

  //fields, setters etc

    public void execute() {
        List<String> fileNames = sftpUtils.getFileNames(downloadPath, downloadRegex);

        for (String name : fileNames) {
            //dowload and process logic
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

现在,在"test execute"中,files.each …

junit mocking spock

4
推荐指数
1
解决办法
5545
查看次数

使用条件api按集合字段的嵌套属性进行搜索

我试图找到所有具有嵌套元素的实体,嵌套元素具有元素集合,我需要通过这些集合的属性找到它.

这将是这样的

class A{
    private B b;
}

class B{
   private Collection<C> cCol;
}

class C{
   private String name;
}
Run Code Online (Sandbox Code Playgroud)

所以我想得到所有具有B元素的A元素,其中C与名称匹配给定参数.

不知道如何使用JPA Critieria API.我知道在JPQL中存在谓词或MEMEBER OF但是我需要按集合中的元素属性进行搜索,而不是集合成员.

尝试之类的东西root.get(a.b.c.name),也有root.fetch(a.b)root.fetch(b.c)但总是结束了与非法API使用一些例外

jpa criteria criteria-api

2
推荐指数
2
解决办法
6170
查看次数

标签 统计

criteria ×1

criteria-api ×1

jpa ×1

junit ×1

mocking ×1

spock ×1