JUnit – A Complete Java Unit Testing Example With Advance Features, Source Code and Diagram

This is a good and thorough tutorial for JUnit Testing I have written so far, where I have tried to accommodate most of the JUnit Features under one Test class and show that to you.

Also, I have written several other articles on JUnit. So don’t forget to read my articles on JUnit Best Practices, why you should use JUnit 4.0 instead of JUnit 3.0, how to performance test your JUnit Test Case, running JUnit as a Test Suite, understanding JUnit annotations¬†and an interesting article on Test Driven Development.

In this article, I aim to make the following stuffs clear when you go through this single post.

  1. Writing a JUnit Test Case
  2. Using JUnit Annotations:
    • @Test
    • @After
    • @Before
    • @AfterClass
    • @BeforeClass
    • @RunWith
    • @Parameter
  3. Performing one Time Setup
  4. Performing one Time TearDown
  5. Normal Setup for each Test Case
  6. Normal TearDown for each Test Case
  7. Exception Handling for expected results

The following JUnit sample test code is written and tested against 4.3.1 version of Junit. Note that there are two classes. MyStuff.java is a class with method which is going to be unit tested. MyStuffTest.java is the real JUnit Test class.

SIMPLE CLASS WHICH WILL BE UNIT TESTED:

package com.kushal.tools;
/**
 * @author Kushal Paudyal
 * This class is very simple class intended to be used by
 * JUnit Test Case for validating various stuffs.
 */
public class MyStuff {

	public double testMethodOne(int a, int b) {
		return a/b;
	}

}

Continue reading

Unit Testing Your Methods By Mocking Private Methods With JMockit Library

So you wrote some public methods which in turn call other private methods and you are asked to write junits for those public methods. But your private method calls do stuffs that you don’t want to test (for example, they might be trying to connect to a database). How would you then write a unit test for you public methods at the same time avoid stuffs that are done in the private methods?

In this tutorial, I will show you how you can use JMockit to mock the private methods. The main magic is, when you are mocking a private method, do not write the access modifier (private).

So instead of trying to mock the method the following way,

@Mock
private boolean doPrivateStuff() {
}

Use this one instead – no private:

@Mock
boolean doPrivateStuff() {
}

Here is the complete code.

package com.kushal.tools;

import mockit.Mock;
import mockit.MockUp;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

/**
 * This class shows you how to Mock a private method.
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * www.icodejava.com
 *
 */

public class PrivateMethodMockTest {

    SomeClass object;
    @Before
    public void setup() {
        object = new SomeClass();
    }
    @SuppressWarnings("unused")
    @Test
    public void test_doPublicStuff_with_mocked_private_method() {
        new MockUp<SomeClass>() {
            @Mock
            boolean doPrivateStuff() {
                System.out.println("This is a mocked private method of class - SomeClass");
                
                /**
                 * Returning false from a mocked method
                 */
                return false; 
            }
        };
        
        boolean result = object.doPublicStuff();
        
        Assert.assertThat(result, CoreMatchers.is(false));
    }
    
    @Test
    public void test_doPublicStuff_with_no_mocking() {
        
        boolean result = object.doPublicStuff();
        
        Assert.assertThat(result, CoreMatchers.is(true));
    }
}

/**
 * This is a demo class whose public method will be tested.
 * Private method can be mocked.
 */
class SomeClass {
    
    public boolean doPublicStuff() {

        System.out.println("Inside doPublicStuff");
        /**
         * Call doPrivateStuff() method from this public method
         */
        return doPrivateStuff();
    }
    
    /**
     * Defining a private method that we want to mock with JMockit
     */
    private boolean doPrivateStuff() {
        System.out.println("Doing Some Private Stuff from Class - SomeClass");
        
        /**
         * Returning true from a real  method.
         */
        return true;
    }
    
}