我有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 …
我试图找到所有具有嵌套元素的实体,嵌套元素具有元素集合,我需要通过这些集合的属性找到它.
这将是这样的
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使用一些例外