JUnit Tutorial

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development, and is one of a family of unit testing frameworks. Its main use is to write repeatable tests for your application code units.

Installation

To include JUnit into your project, you need to include its dependency into classpath.

  • JUnit Maven Dependency

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    
  • JUnit Gradle Dependency

  • dependencies {
      testCompile 'junit:junit:4.12'
    }
    
  • JUnit Jar File

    Click the link to download JUnit 4.12 jar file.

JUnit Annotations

JUnit offers following annotations to write tests.

Annotation Description
@Before The annotated method will be run before each test method in the test class.
@After The annotated method will be run after each test method in the test class.
@BeforeClass The annotated method will be run before all test methods in the test class. This method must be static.
@AfterClass The annotated method will be run after all test methods in the test class. This method must be static.
@Test It is used to mark a method as junit test
@Ignore It is used to disable or ignore a test class or method from test suite.
@FixMethodOrder This class allows the user to choose the order of execution of the methods within a test class.
@Rule Annotates fields that reference rules or methods that return a rule.
@ClassRule Annotates static fields that reference rules or methods that return them.

Writing Tests in JUnit

In JUnit, test methods are marked with @Test annotation. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded.

import java.util.ArrayList;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Example {
	@BeforeClass
	public static void setup() {
	}

	@Before
	public void setupThis() {
	}

	@Test
	public void method() {
		org.junit.Assert.assertTrue(new ArrayList().isEmpty());
	}

	@After
	public void tearThis() {
	}

	@AfterClass
	public static void tear() {
	}
}

Test Suites

Using JUnit test suites, you can run tests spread into multiple test classes. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests.

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)

@Suite.SuiteClasses({
   TestJunit1.class,
   TestJunit2.class
})

public class JunitTestSuite {   
}  

Assertions

Assertions help in validating the expected output with actual output of a testcase. All the assertions are in the org.junit.Assert class. All assert methods are static, it enable better readable code.

import static org.junit.Assert.*;

@Test
public void method() {
	assertTrue(new ArrayList().isEmpty());
}

Assumptions

Assumption indicate the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information. Assume basically means “don’t run this test if these conditions don’t apply”. The default JUnit runner skips tests with failing assumptions.

import org.junit.Test;
import static org.junit.Assume.*;


public class Example {
	public class AppTest {
	    @Test
	    void testOnDev() 
	    {
	        System.setProperty("ENV", "DEV");
	        assumeTrue("DEV".equals(System.getProperty("ENV")));
	    }
	     
	    @Test
	    void testOnProd() 
	    {
	        System.setProperty("ENV", "PROD");
	        assumeFalse("DEV".equals(System.getProperty("ENV")));  
	    }
	}
}

Conclusion

JUnit is undoubtedly most used and robust unit testing framework in java technologies. It has easy learning curve and simple to follow coding practices. Most of IDEs have in-built support for junit tests execution within IDE itself and that make it more developer friendly.

Reference:

JUnit Documentation

Leave a Reply

11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial