Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dernasherbrezon/mockfs
Java FileSystem for simulating IOExceptions
https://github.com/dernasherbrezon/mockfs
filesystem java jdk7 junit mock unit-testing
Last synced: about 13 hours ago
JSON representation
Java FileSystem for simulating IOExceptions
- Host: GitHub
- URL: https://github.com/dernasherbrezon/mockfs
- Owner: dernasherbrezon
- License: apache-2.0
- Created: 2019-04-14T20:39:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-21T20:04:49.000Z (over 3 years ago)
- Last Synced: 2024-05-02T05:09:19.209Z (7 months ago)
- Topics: filesystem, java, jdk7, junit, mock, unit-testing
- Language: Java
- Size: 44.9 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## About [![Build Status](https://travis-ci.com/dernasherbrezon/mockfs.svg?branch=master)](https://travis-ci.com/dernasherbrezon/mockfs) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=com.aerse%3Amockfs&metric=alert_status)](https://sonarcloud.io/dashboard?id=com.aerse%3Amockfs)
Java FileSystem for simulating IOExceptions
## Usage
1. Add maven dependency:
```xml
com.aerse
mockfs
1.4```
2. 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.3. Setup the test
For example:
```java
@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`:
```java
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, ...)`