9

We are currently developing a public API for our app. The methods on this public API are mostly putting together functionality in different corners of the system. An example would be: (names have been changed to protect the innocent)

public String createAppUser(String app, String name, String email, byte[] pwd) { 
     Application appl = applicationModule.retrieve(app);
     User user = userModule.createUser(name,email);
     Digest digest = cryptoModule.createDigest(pwd);
     authenticationModule.authorize(user,appl,digest);
     accountingModule.createAccount(user,appl);
     return user.getId();
}

When unit testing such a thing, the unit test is just a copy of the same workflow, with some additional noise to setup mocks, objects and validate interactions. Although I find unit-test of algorithmic parts of the application essential, in this 'workflow' part, I find unit-test a repetition of the workflow in question form. Following the DRY principle, it feels like a waste of time. What are the best practices in these cases?

Given that these methods are part of a public API, I would find more valuable to test them through a client and validate that the system effects have taken place. This is more 'system test' or 'integration test' and beyond the scope of unit test.

What would be the best test approach in this context?

1 Answer 1

7

You're right: this falls under the 'integration test' category. And with integration tests, I tend to not mock anything other than very external resources (like a web page or network file), since you're supposed to be testing how the real components interact with one another.

In those cases, just assert that the proper application states have been changed and that the output looks fine. In this case running the method and simply confirming that the users table has been modified, that they're authorized, and that you get an ID back.

1
  • Just for the record: I went through the unit tests, mostly checking for pre & post conditions and specially exceptions. I found plenty of issues that needed to be corrected, so although my first impression was 'lost time', my conclusion after the facts is that both types of tests are valuable. Unit tests to ensure that the unit of code is doing as expected and the integration test to ensure that the system response is correct.
    – maasg
    Commented Oct 17, 2011 at 17:02

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.