Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @SpringBootTest decorator导致测试在WatchService中卡住_Java_Spring Boot_Junit5_Watchservice - Fatal编程技术网

Java @SpringBootTest decorator导致测试在WatchService中卡住

Java @SpringBootTest decorator导致测试在WatchService中卡住,java,spring-boot,junit5,watchservice,Java,Spring Boot,Junit5,Watchservice,我有一个简单的应用程序,我想用它监视一个目录,到目前为止,我是通过使用WatchService类来完成的: 应用程序类: @SpringBootApplication public class MmsdirectorywatcherApplication { public static void main(String[] args) { SpringApplication.run(MmsdirectorywatcherApplication.class, args);

我有一个简单的应用程序,我想用它监视一个目录,到目前为止,我是通过使用WatchService类来完成的:

应用程序类:

@SpringBootApplication
public class MmsdirectorywatcherApplication {

    public static void main(String[] args) {
        SpringApplication.run(MmsdirectorywatcherApplication.class, args);
    }

    @Autowired
    DirectoryWatcher directoryWatcher;

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {
            directoryWatcher.startWatching();
        };
    }

}
DirectoryWatcher:

@Component
public class DirectoryWatcher {

    WatchService watchService;
    Path path;
    private String watcherDiretory;

    public DirectoryWatcher() {
    }


    @Value("${mms.directorywatcher.directory}")
    public void setWatcherDiretory(String watcherDiretory) {
        this.watcherDiretory = watcherDiretory;
    }

    public void startWatching(){
        path = Paths.get(watcherDiretory);
        try{
            watchService
                    = FileSystems.getDefault().newWatchService();

            path.register(
                    watchService,
                    StandardWatchEventKinds.ENTRY_CREATE,
                    StandardWatchEventKinds.ENTRY_DELETE,
                    StandardWatchEventKinds.ENTRY_MODIFY);

            WatchKey key;
            while ((key = watchService.take()) != null) {
                for (WatchEvent<?> event : key.pollEvents()) {
                    System.out.println(
                            "Event kind:" + event.kind()
                                    + ". File affected: " + event.context() + ".");
                }
                key.reset();
            }
        }
        catch (IOException ex){

        }
        catch (InterruptedException ex){

        }


    }
}
当我运行测试时,它似乎被卡住了,好像有什么东西阻止了它的完成。可能是watchService本身,但我不确定如何解决这个问题,因为我确实希望最终测试是否调用了startWatching


感谢您抽出时间

您的上下文开始调用
startWatching()
,并且该函数永远不会返回。请注意,
watchService.take()
将一直阻止,直到出现事件

@SpringBootTest(classes = {MmsdirectorywatcherApplication.class})
class MmsdirectorywatcherApplicationTests {
    @Autowired
    private DirectoryWatcher directoryWatcher;
    @Test
    void contextLoads() {
        assertNotNull(directoryWatcher);
    }

}