Nad*_*dir 4 junit mocking spock
我有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 {}打印出预期的内容.但是当调用receiveData.execute()时,我的sftpUtils返回null ..任何想法为什么?
编辑也许我没有说明我的问题 - 我不想只检查是否调用了getFileNames.我需要结果来正确检查for循环.如果我在执行中注释循环,则测试通过.但是因为我使用了getFilenames()方法的结果,所以我得到一个NPE执行方法到达for循环.有了mockito,我会做这样的事情
Mockito.when(sftpUtils.getFilenames(downloadPath, downloadRegex)).thenReturn(filenamesList);
receiveData.execute();
Mockito.verify(sftpUtils).getFilenames(downloadPath, downloadRegex);
//this is what I want to test and resides inside for loop
Mockito.verify(sftpUtils).download(downloadPath, filenamesList.get(0));
Mockito.verify(sftpUtils).delete(downloadPath, filenamesList.get(0));
Run Code Online (Sandbox Code Playgroud)
但我不能在Spock中使用Mockito.verify()然后阻止
Ale*_*ore 13
主要问题是你没有在期望中包含响应生成器(>>部分)(即then:block中的"1*..."部分).
这在spock文档中得到了很好的解释.
您不必在setup:block中声明存根.你可以在then:block中指定一次 - 即使在调用receiveData.execute()之后.由于Groovy AST转换,这是spock神奇的一部分.并且因为(非共享)字段在每次测试之前重新初始化(更多基于AST的魔法),所以在这种情况下甚至不需要setup().
另一件奇怪的事情是,你们都在删除sftpUtils.getFilenames()并从测试代码中调用它.模拟和存根旨在替换从被测系统调用的协作者.没有理由从测试驱动程序调用存根.因此,从给定的块中删除对getFilenames()的调用,让测试中的代码调用它(就像它一样).
Groovy允许您简化对Java set和get方法的调用.看下面的receiveData初始化.可以在Groovy中使用def.让编译器为您找出数据类型.
导致类似于:
class ReceiveDataTest extends Specification {
// only use static for constants with spock
static String PATH = 'downloadPath'
static String REGEX = 'downloadRegex'
def mockSftpUtils = Mock(SftpUtils)
def receiveData = new ReceiveData(downloadPath : PATH,
downloadRegex : REGEX,
sftpUtils : mockSftpUtils)
def "execute() calls getFileNames() exactly once"() {
when:
receiveData.execute()
then:
1 * mockSftpUtils.getFileNames(PATH, REGEX) >> ['file1', 'file2']
0 * mockSftpUtils.getFileNames(_,_)
// The second line asserts that getFileNames() is never called
// with any arguments other than PATH and REGEX, aka strict mocking
// Order matters! If you swap the lines, the more specific rule would never match
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5545 次 |
| 最近记录: |