MockFs

FileSystem for simulating IOExceptions

License

License

GroupId

GroupId

com.aerse
ArtifactId

ArtifactId

mockfs
Last Version

Last Version

1.4
Release Date

Release Date

Type

Type

jar
Description

Description

MockFs
FileSystem for simulating IOExceptions
Project URL

Project URL

https://github.com/dernasherbrezon/mockfs
Source Code Management

Source Code Management

https://github.com/dernasherbrezon/mockfs

Download mockfs

How to add to project

<!-- https://jarcasting.com/artifacts/com.aerse/mockfs/ -->
<dependency>
    <groupId>com.aerse</groupId>
    <artifactId>mockfs</artifactId>
    <version>1.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.aerse/mockfs/
implementation 'com.aerse:mockfs:1.4'
// https://jarcasting.com/artifacts/com.aerse/mockfs/
implementation ("com.aerse:mockfs:1.4")
'com.aerse:mockfs:jar:1.4'
<dependency org="com.aerse" name="mockfs" rev="1.4">
  <artifact name="mockfs" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.aerse', module='mockfs', version='1.4')
)
libraryDependencies += "com.aerse" % "mockfs" % "1.4"
[com.aerse/mockfs "1.4"]

Dependencies

test (1)

Group / Artifact Type Version
junit : junit jar 4.10

Project Modules

There are no modules declared in this project.

About Build Status Quality Gate Status

Java FileSystem for simulating IOExceptions

Usage

  1. Add maven dependency:
<dependency>
  <groupId>com.aerse</groupId>
  <artifactId>mockfs</artifactId>
  <version>1.4</version>
</dependency>
  1. Ensure code is ready
  • Ensure java.nio.file.Path is used instead of java.io.File. FileSystem is available only for Path.
  • Ensure Path::resolve(...) is used instead of java.nio.file.Paths.get(...) or Path::of(...). The latter uses FileSystems.getDefault() instead of mocked
  • All Path should be created from the FileSystem using FileSystem::getPath(...) method.
  1. Setup the test

For example:

	@Test(expected = IOException.class)
	public void testFailingWrite() throws IOException {
		byte[] data = createSampleData();
		Path path = basePath.resolve(UUID.randomUUID().toString());
		mockFs.mock(path, new FailingByteChannelCallback(5));
		try (OutputStream w = Files.newOutputStream(path)) {
			w.write(data);
		}
	}

Features

  • MockFileSystem could transparently pass the data to the default FileSystem. Just use com.aerse.mockfs.NoOpByteChannelCallback:
   	FileSystem fs = FileSystems.getDefault();
   	MockFileSystem mockFs = new MockFileSystem(fs);
   	mockFs.mock(path, new NoOpByteChannelCallback());
  • Use com.aerse.mockfs.FailingByteChannelCallback for simulating IOExceptions after configurable number of bytes.
  • Mock read/write to every file in the directory. Just pass this directory to the com.aerse.mockfs.MockFileSystem::mock(directoryToMock, ...)

Versions

Version
1.4
1.2
1.1