I am trying to write a unit test in Spock to test this verify() method, but Files.getOwner(path) always throws NoSuchFileException exception, how can I prevent this and make owner parameterized in where condition? Tried to stub Files object but no luck.
public void verify(){
try{
Path path = Paths.get("/tmp");
String owner = Files.getOwner(path).getName();
if (owner.equals("root")) {
anotherClass.doSomthing();
}
} catch (IOException e) {
log().error("message");
}
}
Note I cannot use PowerMock or JMockit here and the source code is in Java, test code in Groovy.
On the list of third-party tools you find a link to spock-mockable and other tools.
Not listed there, but usable from Spock 1.x/2.x, JUnit 4/5, TestNG via corresponding extension mechanisms or any other test tool as a Java agent, you can use Sarek, a framework I developed a while ago for myself with the ultimate goal to also better integrate it into the Spock DSL, which never happened due to personal reasons. But it is full-featured and can cover more complex cases than the tools listed on the third-party tools list, e.g. mocking/modifying constructors, JVM bootstrap classes, static initialiser blocks. It is a bit unweildy, but very powerful. Check out the Javadocs and the tests in each module in order to find out how to use the
Probably spock-mockable is good enough for most use cases and easy to apply via annotations. If you need more power, feel free to contact me about Sarek, if you cannot find out how to solve your specific use case.