- Mastering Software Testing with JUnit 5
- Boni García
- 301字
- 2021-07-02 15:34:24
Unit testing
Unit testing is a method by which individual pieces of source code are tested to verify that the design and implementation for that unit have been correctly implemented. There are four phases executed in sequence in a unit test case are the following:
- Setup: The test case initializes the test fixture, that is the before picture required for the SUT to exhibit the expected behavior.
- Exercise: The test case interacts with the SUT, getting some outcome from it as a result. The SUT usually queries another component, named the Depended-On Component (DOC).
- Verify: The test case determines whether the expected outcome has been obtained using assertions (also known as predicates).
- Teardown: The test case tears down the test fixture to put the SUT back into the initial state.
These phases and its relationship with the SUT and DOC is illustrated as follows:

Unit test generic structure
Unit testing is done with the unit under test in isolation, that is, without interacting its DOCs. To that aim, test doubles are employed to replace any components on which the SUT depends. There are several kinds of test doubles:
- A dummy object simply satisfies the real object API but it is never actually used. The typical use case for dummy objects is when they are passed as parameters to meet the method signature, but then the dummy object is not actually used.
- A fake object replaces the real object with a simpler implementation, for example, an in-memory database.
- A stub object replaces the real object providing hard-coded values as responses.
- A mock object also replaces the real object, but this time with programmed expectations as responses.
- A spy object is a partial mock object, meaning that some of its methods are programmed with expectations, but the others use the real object's implementation.
推薦閱讀
- 劍指JVM:虛擬機(jī)實(shí)踐與性能調(diào)優(yōu)
- SQL Server 2012數(shù)據(jù)庫技術(shù)及應(yīng)用(微課版·第5版)
- Machine Learning with R Cookbook(Second Edition)
- Data Analysis with Stata
- Microsoft System Center Orchestrator 2012 R2 Essentials
- SSM輕量級(jí)框架應(yīng)用實(shí)戰(zhàn)
- Visual FoxPro程序設(shè)計(jì)習(xí)題集及實(shí)驗(yàn)指導(dǎo)(第四版)
- Arduino計(jì)算機(jī)視覺編程
- GitHub入門與實(shí)踐
- Python Machine Learning Blueprints:Intuitive data projects you can relate to
- 計(jì)算語言學(xué)導(dǎo)論
- 3D Printing Designs:The Sun Puzzle
- C語言從入門到精通(微視頻精編版)
- Go語言編程之旅:一起用Go做項(xiàng)目
- Visual C++ 開發(fā)從入門到精通