Unit Testing is important part of software which is done by Developer which qualifies the software. Software passes through different kind of testing during its life and Unit testing is one of them.Unit testing means testing each unit, each line of code.Integration testing is also useful but this blog will focus on Unit testing only.

There are many frameworks available for unit testing like Mockito, EasyMock, PowerMockito etc. All frameworks provide more or less similar APIs to write test cases. One of the important concept which Developers are confused about Mocking when writing jUnit(It’s Java unit testing library so it stars with j 🙂 ) test. I will be on it to clarify you with blogs.

Why to write unit test case ? What is mock ? What to mock ? Why to mock ? When to mock ? are the questions which comes up when you start with jUnit testing. Let’s go slowly and start with why to write test case.

Why to write unit test case ?

I believe, writing unit test case of each line of code is every Developer’s responsibility.Let say you have write simple code.

Unit of Code

public int calculateSalary(Boolean onTravel) {
    Double basic = 100.0;
    Double travelAllowance = 100.0;
    Double foodAllowance = 100.0;
    Double monthlyAllowance = 100.0;

    Double total = basic+ monthlyAllowance +foodAllowance;
    if(onTravel) {
        total = total + travelAllowance;

    return total;

This method takes Boolean specifying if employee is on travel or not and returns total salary. You need to write test case so that it will check if implementation/functionality works correctly or not.

To be sure if functionality works correctly or not, you need to assert what is expected and what is actually returned by unit of code.Test case fails if assertion fails means what you are expecting is not equal to what you are actually getting after running test case.

To test the functionality you obviously need test data which is very important part of writing test case. If you pass true then it will return $550, if you pass else then it will return $400. It means if you want to provide 100% coverage of this particular unit of code then you must have to write two test cases, one with passing true value and another with false.

You need to write those many number of test cases which cover all the scenarios.Writing the test case with functionality in mind normally covers all the possible scenarios and never leave any edge case uncover.

This particular example returns only one value that you need to assert.This is the simplest  test case and below is the assertion.

Assert.assertEquals(400, total);

You might need to write test case which returns custom object.In this case, you need to assert all the possible attributes of that custom object affected by unit of code. Ideally, apart from affecting attributes you should assert all the attributes of custom object. For example, if unit of code updates employee’s salary in custom object Employee, so you need to assert expected salary that is going to be updated by the unit of code.

Assert.assertEquals(500, customEmployeeObject.getSalary());


You can also assert what exception will be thrown by unit of code and assert it too.This way you can cover try..catch block of unit of code.It is very good practice if you assert all the possible exceptions that can be thrown by writing test case with those specific data.

One interesting case is when unit of block for which you are writing test case does not return any thing. How will you assert as it is returning nothing ? There are certain ways to assert it too which I will cover in the upcoming blogs.

Hopefully, you got the answer of why to write test case.You have still unanswered questions related to Mock which I will cover in the upcoming blogs.

Reference links :



If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on social media. Thank you!