Book Image

Mastering Unit Testing Using Mockito and JUnit

By : Sujoy Acharya
Book Image

Mastering Unit Testing Using Mockito and JUnit

By: Sujoy Acharya

Overview of this book

Table of Contents (17 chapters)
Mastering Unit Testing Using Mockito and JUnit
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Chapter 10. Best Practices

"It is insanity to keep doing things the same way and expect things to improve."

—Anonymous

Writing clean, readable, and maintainable JUnit test cases, just like writing clean code, is an art. A well-written unit test can prevent maintenance nightmare and acts as a system documentation; however, if not used carefully, it can produce meaningless boilerplate test cases. Mistakes are part of the learning process as long as you aren't making them repeatedly. JUnit is not rocket science, so we can practice, follow guidelines, and learn from others to make it perfect.

This chapter covers JUnit guidelines and best practices. The following categories are covered in depth:

  • Writing meaningful tests

  • Test automation

  • Test configuration

  • Assertion convention

  • Exception handling

  • Test smells and refactoring test smells