How can I spy for the Class File in anthoer function in class?
Works for the test when I use new File in the test but not in my class.
public class Clazz {
public void fun(String path) {
File file = new File(path);
if (!file.exists()) {
throw new FileNotFoundException();
}
}
}
//Spock-test
class test extends Specification {
given:
GroovySpy(File, global: true, useObjenesis: true)
def mockFile = Mock(File) {
exists() >> true
}
new File(path) >> {mockFile}
when:
Clazz.fun("test.file")
then:
...
...
}