Set DefaultFileSystemProvider for testing

How can I set DefaultFileSystemProvider to use, for example, JimfsFileSystemProvider ? The javadoc for FileSystems.getDefault() says I need to set a system property, but when I try to do this, I get a NoSuchMethodException :

 System.setProperty("java.nio.file.spi.DefaultFileSystemProvider", "com.google.common.jimfs.JimfsFileSystemProvider"); FileSystems.getDefault(); 

Stack trace:

 java.lang.Error: java.lang.NoSuchMethodException: com.google.common.jimfs.JimfsFileSystemProvider.<init>(java.nio.file.spi.FileSystemProvider) at java.nio.file.FileSystems$DefaultFileSystemHolder.getDefaultProvider(FileSystems.java:128) .... 

Do I need to configure something else or is this a bug in jimfs?

+5
source share
1 answer

The javadoc FileSystems.getDefault() states that:

... by default, a FileSystemProvider is created by calling a constructor with one argument, the formal parameter type of which is FileSystemProvider .

Since JimfsFileSystemProvider does not have such a constructor, you cannot set it as the default file system.

This is exactly what the error means:

 java.lang.Error: java.lang.NoSuchMethodException: com.google.common.jimfs.JimfsFileSystemProvider.<init>(java.nio.file.spi.FileSystemProvider) 

The <init> method is a constructor, and the constructor was not found with the parameters java.nio.file.spi.FileSystemProvider .

+3
source

Source: https://habr.com/ru/post/1202230/


All Articles