Unit test logic with multiple classes

I have a subscription class with an evaluation method. These methods get a plan for this subscription (as a model), then it gets paid for it. In this case, the subscription describes an invoice object that contains payments that have not been issued since the last invoice date.

I would like to test this method, but it seems to me that it will not be unit test, since it will include many objects with various dependencies.

How would you test this method?

+4
source share
2 answers

This is not a unit test for purists (rather an integration test), but still it can be a completely subtle test :-) And technically you can run it using JUnit (or depending on your favorite unit testing system) so IMHO the difference is only in terminology .

If you write your code from scratch, it’s best to start by writing unit tests for individual methods in isolation (dependency bullying), and then in the next step, perhaps add higher-level integration tests, such as those that you describe, to make sure that your classes work well together.

However, in older projects (i.e. a lot of legacy code without tests) it is often impractical to start with fine-grained low-level unit tests; instead, it’s more efficient to write more complex and more complex tests that refine and “block” the behavior of the larger component.

Unfortunately, most of the projects in this industry today are a legacy :-( For me, in these cases, pragmatism wins for the cleanliness of the hands-down approach :-)

+10
source

Be that as it may, it sounds like an integration test, not a unit test.

If you want the unit test to use methods, you must mock the dependencies (so you use the Mock Invoice to return known data). You can then write separate tests separately to make sure the invoice class is working.

+2
source

Source: https://habr.com/ru/post/1335957/


All Articles