3

I find it easy to write unit tests for algorithms. For example, sort(List), it is easy to write tests like:

list = [2, 1, 4];
assert(sort(list) == [1, 2, 4]);

But I find it really hard to test methods that have no logic, no if statements, just a set of calls.

There are mainly 2 examples that I'd like an answer for how to unit test them:

Example 1:

Let's say I have a class that is responsible for writing some data to a file, but that data is written in a specific way by external functions (writeHeaderToFile, writeSerializedData and writeEndOfFile).

The data is not written straight as it is to the file, so if data is something like:

{
    list: [
        "item 1",
        "item 2"
    ],
    name: "aaa"
}

That doesn't mean that the file will be neither the plain version of that data (without white spaces) nor it will be a simple serialized version or encrypted version into file. The actual file binary will be something unknown to me. All I know is that I can use those 3 methods to write in the right way.

This file also contains some other information that doesn't come directly from those 3 methods, like a specific type of header (that again, I have no idea how it will be represented in the file binary).

That is the class:

class FileCreator {
    populateFileWithData(File file, Data data) {
        doBlockWithLock(file, {
            Header header;
            header.format = SomeFormat;
            header.version = SomeVersion;
            writeHeaderToFile(file, header);

            writeSerializedData(file, data);

            writeEndOfFile(file);
        });
    }

    // Private
    void doBlockWithLock(File file, Lambda block) {
        file.holdWritingLock();
        block();
        file.releaseWritingLock();
    }
}

Example 2:

class Controller {

    var screenOne = new ScreenOne();
    var screenTwo = new ScreenTwo();
    var screenThree = new ScreenThree();

    void reloadButtonWasClicked() {
        screenOne.reload();
        screenTwo.reload();
        screenThree.reload();
    }
}

For this one I could do something like this:

var mockScreenOne = Mock<ScreenOne>().reload.expectOneCall();
var mockScreenTwo = Mock<ScreenTwo>().reload.expectOneCall();
var mockScreenThree = Mock<ScreenThree>().reload.expectOneCall();

Controller controller = new Controller();
controller.screenOne = mockScreenOne;
controller.screenTwo = mockScreenTwo;
controller.screenThree = mockScreenThree;

controller.reloadButtonWasClicked();

mockScreenOne.verify();
mockScreenTwo.verify();
mockScreenThree.verify();

But I don't find much value in it since I'm just asserting that I'm doing the same thing I'm doing in the implementation. Seems like code repetition to me.


What would be the proper way of testing my 2 examples?

10
  • Leaving aside the last example, in the first, are you saying that you have no idea what writeSerializedData or the other method will do? That you have no idea what sort of fileFileCreator will produce, perhaps because those methods are part if another codebase or something? Apr 16, 2015 at 3:15
  • Exactly! I do have some idea and it is "my" code base, I just don't have the life time that it would take to understand it all, if you know what I mean... Apr 16, 2015 at 3:51
  • If i were testing it, i would simply test that the FileCreator.populateFileWithData completes without throwing an exception. I would also check that the target file exists (since it should exist), before i delete my temporary test file. For the second one, i would be checking each screen that it reacted appropriately to the reload; did it fill in stuff it was supposed to without error.
    – Ian Boyd
    Apr 22, 2015 at 2:02
  • @RodrigoRuiz What might be more valuable in a situation like this is integration tests rather than unit tests. Does the FileCreator with an implementation of the serializer produce the correct file? Does clicking the reload button produce the correct behaviour in the view? However, because of the simplicity of the classes, they could probably go untested. They are so simple that either they work or do not. You would probably notice the presence of bugs by simply using the app. Apr 22, 2015 at 18:34
  • @thalesmello So basically you would not test classes with no logic like my Controller? Apr 24, 2015 at 4:12

1 Answer 1

1

In the first example, if you wrote the messages in question and satisfied with your test coverage, there's no reason to reproduce that testing logic on FileCreator. You just need to test the FileCreator populateFileWithData method to make sure the file is written and maybe that the locking mechanism works.

You are right, your last test is rather trivial. I'd be tempted to omit writing it. But it depends. Is it likely that someone might come along and comment out one of those panel constructors? Do you have other tests that would identify such a problem?

1
  • The similarity between them is that they both have no logic. 1) Let's assume those 3 methods are already tested and are not mine. If I don't have access to the deserialize function, how would I test it? Also, let's say that I'm the one creating the deserialize function, so it is a separate logic that I don't wanna replicate in my unit tests for the populateFileWithData method 2) By omit you mean not test it? Apr 16, 2015 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.