简体   繁体   English

监视Mockito中的路径

[英]Spying on a Path in Mockito

I'm trying to spy on a Path using Mockito 我正在尝试使用Mockito 监视 路径

dirSpy = spy(Files.createTempDirectory(DIR_NAME));

and I get an error saying 我收到一个错误消息

Mockito cannot mock this class: class sun.nio.fs.UnixPath Mockito无法模拟此类:类sun.nio.fs.UnixPath
Mockito can only mock visible & non-final classes. Mockito只能模拟可见和非最终类。
If you're not sure why you're getting this error, please report to the mailing list. 如果不确定为什么会收到此错误,请向邮件列表报告。

[I can post a longer stack trace if you think it is needed] [如果您认为有必要,我可以发布更长的堆栈跟踪记录]

I assume that this is because the Path has a native implementation - my question is if there is some workaround that will enable me to check if a specific location was accessed during a test 我认为这是因为Path具有本机实现-我的问题是是否存在一些变通办法,使我能够检查测试期间是否访问了特定位置

what I'm trying to test- I have a directories structure for each user - I wanted to do a test that checks that in the case that there are no users, there isn't a redundant work done and that the root of all users isn't accessed. 我要测试的内容 -每个用户都有一个目录结构-我想做一个测试,以检查在没有用户的情况下,没有多余的工作以及所有用户的根无法访问。

In my over whole test logic this is a minor verification and i can do without it - just thought it's something that is nice to have 在我的整体测试逻辑中,这是一个次要验证,我可以不用它-只是觉得这很不错

my question is if there is some workaround that will enable me to check if a specific location was accessed during a test 我的问题是是否有一些解决方法可以使我检查测试期间是否访问了特定位置

Yes, using a WatchService . 是的,使用WatchService

Note that should you be able to do it, watching the path itself won't bring you much: operations on Path s don't do I/O except for .toRealPath() if called at all and if the filesystem has symlinks support (OK, you use some Unix variant, so that is likely to be the case). 需要注意的是,你应该能够做到这一点,看着路径本身不会给你带来太多:上操作Path ■不要做I / O除外.toRealPath() 如果调用所有, 如果文件系统有符号链接的支持(好的,您使用一些Unix变体,所以很可能是这种情况。 The real I/O is done by the path's FileSystemProvider . 实际的I / O由路径的FileSystemProvider But you won't be able to spy that either... 但是您也不会监视...

A WatchService is therefore your best solution. 因此, WatchService是您最好的解决方案。

See here for an example. 请参阅此处的示例。 And please note that a WatchService is filesystem dependent; 并且请注意, WatchService依赖于文件系统; this means you are still better off doing file I/O from a class of yours which you can spy ;) 这意味着您最好还是从您可以监视的类中进行文件I / O;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM