FileMock is a simple yet powerful java library to convert Jason file into objects while writing unit tests. Most of the time writing unit test made difficult by creating nested service response. creating a proper input object may take hundreds of lines and hours of dev effort. FileMock
is designed to reduce this effort by simply saving the response as JSON files and annotate the path in your test file.
<dependency>
<groupId>io.github.kvlabs.filemock</groupId>
<artifactId>core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>io.github.kvlabs.filemock</groupId>
<artifactId>junit</artifactId>
<version>1.0.0</version>
</dependency>
Annotate the object need to be mocked with @MockFile
and invoke initMocks
on test case setup
public class SampleTest {
@MockFile(path = "/META-INF/mock/v1/get_item_response.json")
SampleResponse sample;
@Before // JUnit
@BeforeMethod(alwaysRun=true) // TestNG
public void setUp() {
FileMockAnnotations.initMocks(this);
}
@Test
public void testInitMocks() {
Assert.assertNotNull("sample response got set ", sample);
}
}
Annotate the object need to be mocked with @MockFile
and run with FileMockJUnitRunner
@RunWith(FileMockJUnitRunner.class)
public class SampleTest {
@MockFile(path = "/META-INF/mock/v1/get_item_response.json")
SampleResponse sample;
@Test
public void testInitMocks() {
Assert.assertNotNull("sample response got set ", sample);
}
}
Use FileMock.mockFile
function straight in the test method
import static io.kvlabs.filemock.core.FileMock.mockFile;
public class SampleTest {
@Test
public void testInitMocks() {
SampleResponse sample = mockFile("/META-INF/mock/v1/get_item_response.json", SampleResponse.class);
Assert.assertNotNull("sample response got set ", sample);
}
}
By contributing your code, you agree to license your contribution under the MIT License.