javaspring-bootjunit5watchservice

@SpringBootTest decorator causes test to get stuck for WatchService


I have simple application which I want to use to watch over a directory, the way I have done it so far is by using WatchService class:

ApplicationClass:

@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){

        }


    }
}

This run fine, but I want to test if the applicationStarted correctly, here is the test:

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

}

When I run the test it seems like its stuck like something is blocking it from completion. May be it the watchService itself, but I am not sure how to get around this as I do want to eventually test if startWatching is been called.

Thanks for you time


Solution

  • Your context start calls startWatching(), and that function just never returns. Note that watchService.take() will block until an event comes in.