Next week will be my first time writing unit tests at work. People at work said unit tests, and proposed to cover several methods of related classes in each test method.
But you said unit tests! I had no idea what they meant by unit. After some searching, I found a blog post that perfectly answered my question: “the team decides what makes sense to be a unit.”
So to my 1st question: I guess my team regards a unit as a single business function (e.g. loading a list of customer info). That may justify covering a presenter method and related methods from other classes in a single test method. That also means I won’t need to substitute every collaborating class with TestDoubles..
Two more questions left:
- The app I’m developing interacts with a remote server. How should I design TestDouble to mock server?
(Perhaps i should take different approach in each test method..) - For Android app developers, what would they consider a unit to be?
(Curious in general how they would write local unit test codes..)
To get a sense of how to write unit tests, I’m reading a book on test code styles (Effective Unit Testing). Still in the middle of the book, but I got a few useful points:
- 1-hour intro to JUnit (Test Class; Test Method–and Before, BeforeClass, After, AfterClass; expecting Exceptions; assertThat; hamcrest and custom matchers)
- Incremental process of TDD : Write test codes until they fail –> Develop required part –> See if it passes the test –> Refactor the codes and the test –> Repeat..
- Good test codes are readable, independent, and reliable (“deterministic”).
- There are different types of TestDoubles. Customize one that’s simple and good enough to serve your test (don’t over-equip TestDoubles).