How to test IO code in JUnit?
I'm want to test two services:
- service which builds file name
- service which writes some data into file provided by 1st service
In first i'm building some complex file structure (just for example {user}/{date}/{time}/{generatedId}.bin
)
In second i'm writing data to the file passed by first service (1st service calls 2nd service)
How can I test both services using mocks without making any real IO interractions?
Just for example:
1st service:
public class DefaultLogService implements LogService
{
public void log(SomeComplexData data)
{
serializer.write(new FileOutputStream(buildComplexFileStructure()), data);
or
serializer.write(buildComplexFileStructure(), data);
or
serializer.write(new GenericInputEntity(buildComplexFileStructure()), data);
}
private ComplextDataSerializer serializer; // mocked in tests
}
2nd service:
public class DefaultComplexDataSerializer implements ComplexDataSerializer
{
v开发者_运维百科oid write(InputStream stream, SomeComplexData data) {...}
or
void write(File file, SomeCompexData data) {...}
or
void write(GenericInputEntity entity, SomeComplexData data) {...}
}
In first case i need to pass FileOutputStream which will create a file (i.e. i can't test 1st service)
In second case i need to pass File. What can i do in 2nd service test if I need to test data which will be written to specified file? (i can't test 2nd service)
In third case i think i need some generic IO object which will wrap File. Maybe there is some ready-to-use solution for this purpose?
In the first case, why do you care about the file itself, if you're testing file name generation? You don't want to test FileOutputStream.
I'm assuming your method looks something like this:
public File buildComplexFileStructure() {
// code
}
Your test would look something like this:
@Test public void test() throws Exception {
File expected = <what you expect to be generated>;
File actual = new DefaultLogService().buildComplexFileStructure();
assertEquals(expected, actual);
}
If that method isn't exposed publicly, then just expose it to the test (default or protected).
In the second case, couldn't you pass a ByteArrayOutputStream from your test test into your method, then verify what was written to it? i.e.:
@Test public void test() throws Exception {
btye[] expected = <expected data>;
ByteArrayOutputStream actualStream = new ByteArrayOutputStream();
new DefaultComplexDataSerializer().write(actualStream, data);
byte[] actualData = actualStream.toByteArray();
// compare expected and actualData
}
精彩评论