3

I'm want to test two services:

  1. service which builds file name
  2. 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
{

    void 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?

acc15
  • 1,205
  • 1
  • 11
  • 22
  • possible duplicate of [How do I unit-test saving file to the disk?](http://stackoverflow.com/questions/3381801/how-do-i-unit-test-saving-file-to-the-disk) – Raedwald Jul 05 '14 at 13:37

1 Answers1

5

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
}
Kaypro II
  • 3,210
  • 8
  • 30
  • 41
  • in this case i will lose my 100% test coverage stat =( – acc15 Jun 21 '11 at 20:24
  • 2
    @acc15 aiming for 100% coverage is actually a bad thing... do not enter the metric game :) You must test what's important, not every single class. My two cents :) – Charles Morin Dec 04 '17 at 20:16