Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unit test logic involving multiple classes

Tags:

unit-testing

I have a subscription class with an assess method. This methods gets the plan for this subscription (as an model) then this gets the fees for it. With this the subscription contructs an invoice object that contains the fees that were not billed from the last billing date.

I would like to test this method but it seems to me that this would not be an unit test since it would involve many object with different dependencies.

How would you test this method ?

like image 963
Gabriel Solomon Avatar asked Jan 18 '11 08:01

Gabriel Solomon


1 Answers

It is not a unit test for purists (rather an integration test), but still it may be a perfectly fine test :-) And technically you can run it with JUnit (or whichever your favourite unit testing framework is), so IMHO the difference is only in terminology.

If you write your code from scratch, it is best indeed to start by writing unit tests for individual methods in isolation (mocking out dependencies), then at the next stage maybe add higher level integration tests such as the one you describe, to verify that your classes work together well.

However, in legacy projects (i.e. lots of inherited code without tests), it is often not feasible to start with fine grained low-level unit tests; instead it is more efficient to write higher-level, more complex tests which clarify and "lock" the behaviour of a larger component.

Unfortunately the majority of projects in this industry by far is legacy :-( For me, in these cases, pragmatism wins over purity of approach hands down :-)

like image 80
Péter Török Avatar answered Nov 03 '22 00:11

Péter Török