Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MASTERING UNIT TESTING USING MOCKITO AND JUNIT

You're reading from   MASTERING UNIT TESTING USING MOCKITO AND JUNIT An advanced guide to mastering unit testing using Mockito and JUnit

Arrow left icon
Product type Paperback
Published in Jul 2014
Publisher
ISBN-13 9781783982509
Length 314 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Sujoy Acharya Sujoy Acharya
Author Profile Icon Sujoy Acharya
Sujoy Acharya
Arrow right icon
View More author details
Toc

Defining unit testing

A test is an assessment of our knowledge, a proof of concept, or an examination of data. A class test is an examination of our knowledge to ascertain whether we can go to the next level. For software, it is the validation of functional and nonfunctional requirements before it is shipped to a customer.

Unit testing code means validation or performing the sanity check of code. Sanity check is a basic test to quickly evaluate whether the result of a calculation can possibly be true. It is a simple check to see whether the produced material is coherent.

It's a common practice to unit test the code using print statements in the main method or by executing the application. Neither of them is the correct approach. Mixing up production code with tests is not a good practice. Testing logic in the production code is a code smell, though it doesn't break the code under the test. However, this increases the complexity of the code and can create severe maintenance problem or cause system failure if anything gets misconfigured. Print statements or logging statements are executed in the production system and print unnecessary information. They increase execution time and reduce code readability. Also, junk logging information can hide a real problem, for instance, you may overlook a critical deadlock or a hung thread warning because of excessive logging of junk.

Unit testing is a common practice in test-driven development (TDD). TDD is an evolutionary development approach. It offers test-first development where the production code is written only to satisfy a test, and the code is refactored to improve its quality. In TDD, unit tests drive the design. You write code to satisfy a failing test, so it limits the code you write to only what is needed. The tests provide a fast, automated regression for refactoring and new enhancements.

Kent Beck is the originator of Extreme Programming and TDD. He has authored many books and papers.

Generally, all tests are included in the same project but under a different directory/folder. Thus, a org.packt.Bar.java class will have a org.packt.BarTest.java test. These will be in the same package (org.packt) but will be organized in the: src/org/foo/Bar.java and test/org/foo/BarTest.java directories, respectively.

Our customers do not execute the unit tests, so we don't deliver the test source folder to them. Having the code and test in the same package allows the test to access protected and default methods/properties. This is particularly useful while working with the legacy code.

Java code can be unit tested using a code-driven unit testing framework. The following are a few of the available code-driven unit testing frameworks for Java:

  • SpryTest
  • Jtest
  • JUnit
  • TestNG

JUnit is the most popular and widely used unit testing framework for Java. We will explore JUnit 4 in the next section.

You have been reading a chapter from
MASTERING UNIT TESTING USING MOCKITO AND JUNIT
Published in: Jul 2014
Publisher:
ISBN-13: 9781783982509
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image