title: Unit testing WatchService
body: <p>How can I unit test a watchservice like this:</p>
  
  <pre><code>/**
   * Function to look for changes in a determinate directory
   * @param directory is the directory where to look for the changes
   * @return return a boolean that is true if have changes or false if not
   */
  
  private static boolean watchDirectory(final String directory){
  
      boolean flag = false;
  
      Path path = Paths.get(directory);
      try {
          // get watch service which will monitor the directory
          WatchService watcher = path.getFileSystem().newWatchService();
          // associate watch service with the directory to listen to the event types
          path.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
          System.out.println("\nMonitoring directory for changes...");
          // listen to events
          WatchKey watchKey = watcher.take();
          // get list of events as they occur
          List&lt;WatchEvent&lt;?&gt;&gt; events = watchKey.pollEvents();
  
          for (WatchEvent event : events) {
              flag = false;
              //check if the events refers to a new file created
              if (event.kind() == StandardWatchEventKinds.ENTRY_CREATE) {
                  flag = true;
  
                  System.out.println("Created: " + event.context().toString() + " ;");
              }
          }
      } catch (IOException | InterruptedException e) {
          e.printStackTrace();
      }
      return flag;
  }
  </code></pre>
  
  <p>It is looking for changes in a directory, if a new file is created it returns a flag with value true, if i didnt find changes return the flag as false.</p>