Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying MASTERING UNIT TESTING USING MOCKITO AND JUNIT
  • Table Of Contents Toc
  • Feedback & Rating feedback
MASTERING UNIT TESTING USING MOCKITO AND JUNIT

MASTERING UNIT TESTING USING MOCKITO AND JUNIT

By : Acharya
4 (11)
close
close
MASTERING UNIT TESTING USING MOCKITO AND JUNIT

MASTERING UNIT TESTING USING MOCKITO AND JUNIT

4 (11)
By: Acharya

Overview of this book

A practical and easy-to-follow, yet comprehensive, guide to learning advanced JUnit testing. Each topic is explained and placed in context, and for the more inquisitive, there are more details of the concepts used. This book is for you if you are a developer with some experience in Java application development as well as a basic knowledge of JUnit testing. But for those whose skill set is void of any prior experience with JUnit testing, the book also covers basic fundamentals to get you acquainted with the concepts before putting them into practise.
Table of Contents (12 chapters)
close
close
11
Index

Handling exceptions


Exception handling is an important part of Java coding. The Java community follows a set of best practices about exception handling. The following are the exception handling best practices for unit testing:

  • Do not write catch blocks to pass a unit test. Consider the following example where a Calculator program has a divide method. It takes two integers, divides, and returns a result. When divide encounters a divide by zero, the program should throw an exception. The following is the code:

    public class Calculator {
    
      public int divide(int op1, int op2)  {
        return op1/op2;
      }
    }

    The following is the test:

    @Test
    public void divideByZero_throws_exception() throws Exception {
      try {
        calc.divide(1, 0);
        fail("Should not reach here");
      } catch (ArithmeticException e) {
    
      }
    }

    Instead of catching ArithmeticException, we can apply the JUnit 4 pattern as follows:

      @Test(expected = ArithmeticException.class)
      public void divideByZero_throws_exception() throws Exception...
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
MASTERING UNIT TESTING USING MOCKITO AND JUNIT
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon