Skip to content

Introduction to JUnit (Video)

Learn the basics of JUnit, the Java unit testing framework, with a video tutorial, a table of assert methods and the full example code to download.

JUnit is a framework used in programming to write unit tests for applications developed in Java.

It is common in software development to define a good test plan before the implementation phase (either during analysis or design) to check that the code works correctly and meets the requirements and specifications defined for the software product you want to build.

In this video tutorial we will run a series of unit tests on some previously written code, in order to show the framework and see some of its features.

The code we will unit test is a class that represents a complex number and performs addition, subtraction, multiplication, division and modulus, besides having some other “absurd” methods created for this tutorial for teaching purposes.

You can download the project for this video tutorial HERE.

Below is a table summarizing the checks you can do with JUnit:

JUnit assertxxx() method What it checks
assertTrue(expression) checks that expression evaluates to true
assertFalse(expression) checks that expression evaluates to false
assertEquals(expected,actual) checks that expected equals actual
assertNull(object) checks that object is null
assertNotNull(object) checks that object is not null
assertSame(expected_object,actual_object) checks that expected_object and actual_object are the
same object
assertNotSame(expected_object,actual_object) checks that expected_object is not the same object
as actual_object
fail() makes the test end with a failure

Link to the JUnit Javadoc: http://www.dc.fi.udc.es/ai/tp/practica/junit/javadoc/junit/framework/Assert.html

Code to be tested:


package Main;
public class NumComplejo {

	private double pReal;
	private double pImaginaria;

	public NumComplejo(){

	}

	public NumComplejo(double pReal, double pImaginaria){
		this.pReal=pReal;
		this.pImaginaria=pImaginaria;
	}

	public double getpReal() {
		return pReal;
	}

	public void setpReal(double pReal) {
		this.pReal = pReal;
	}

	public double getpImaginaria() {
		return pImaginaria;
	}

	public void setpImaginaria(double pImaginaria) {
		this.pImaginaria = pImaginaria;
	}

	/**
	 * Adds two complex numbers
	 * @param c1
	 * @param c2
	 * @return sum
	 */
	public NumComplejo sumarComplejos (NumComplejo c1, NumComplejo c2){
		NumComplejo result = new NumComplejo();

		result.setpReal(c1.getpReal()+c2.getpReal());
		result.setpImaginaria(c1.getpImaginaria()+c2.getpImaginaria());

		return result;
	}

	/**
	 * Subtracts two complex numbers
	 * @param c1
	 * @param c2
	 * @return difference
	 */
	public NumComplejo restarComplejos (NumComplejo c1, NumComplejo c2){
		NumComplejo result = new NumComplejo();

		result.setpReal(c1.getpReal()-c2.getpReal());
		result.setpImaginaria(c1.getpImaginaria()-c2.getpImaginaria());

		return result;
	}

	/**
	 * Multiplies two complex numbers
	 * @param c1
	 * @param c2
	 * @return product
	 */
	public NumComplejo multiplicarComplejos (NumComplejo c1, NumComplejo c2){
		NumComplejo result = new NumComplejo();

		double a=c1.getpReal();
		double b=c1.getpImaginaria();
		double c=c2.getpReal();
		double d=c2.getpImaginaria();

		result.setpReal((a*c)-(b*d));
		result.setpImaginaria((a*d)+(b*c));

		return result;
	}

	/**
	 * Divides two complex numbers
	 * @param c1
	 * @param c2
	 * @return quotient
	 */
	public NumComplejo dividirComplejos (NumComplejo c1, NumComplejo c2){
		NumComplejo result = new NumComplejo();

		double a=c1.getpReal();
		double b=c1.getpImaginaria();
		double c=c2.getpReal();
		double d=c2.getpImaginaria();

		double dividendoReal = (a*c)+(b*d);
		double dividendoImaginario = (b*c)-(a*d);
		double divisor = (Math.pow(c, 2)+Math.pow(d, 2));

		result.setpReal(dividendoReal/divisor);
		result.setpImaginaria(dividendoImaginario/divisor);

		return result;
	}

	/**
	 * Calculates the modulus of a complex number
	 * @param numComplejo
	 * @return modulus
	 */
	public double modulo (NumComplejo numComplejo){
		double real =  numComplejo.getpReal();
		double imaginaria = numComplejo.getpImaginaria();

		return Math.sqrt(Math.pow(real, 2)+Math.pow(imaginaria, 2));
	}

	@Override
	public String toString(){
		if (pImaginaria>=0){
			return pReal+"+"+pImaginaria+"i";
		}else
			return pReal+"-"+(pImaginaria*(-1))+"i";
	}

	/**********************************************************************/
	/****** Absurd methods to be able to show an example with Junit ******/
	/**********************************************************************/
	// Method that returns true if the number passed in is positive
	// or false if it is negative
	public boolean esPositivo(double num){
		if(num>0)
			return true;
			else
				return false;
	}

	/**
	 * Absurd method that returns an array
	 * @param c1
	 * @return
	 */
	public double [] getUnArray (NumComplejo c1){
		double [] nums = new double [2];
		nums[0]=c1.getpReal();
		nums[1]=c1.getpImaginaria();
		return nums;
	}

	/**
	 * ANOTHER absurd method that returns an array of size 2 after adding 1 to the first element
	 * @param c1
	 * @return
	 */
	public double [] getOtroArray (double [] array){
		array[0] ++;
		return array;
	}

	/**
	 * Absurd method that takes 2 seconds to return the result
	 * @return
	 * @throws InterruptedException
	 */
	public int time () throws InterruptedException{
		Thread.sleep(4000);
		return 1;
	}

}

Test implementation:


package Main;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

public class pruebasUnitarias {

	private NumComplejo c1;
	private NumComplejo c2 = new NumComplejo(1,1);
	private double [] nums = {1,0};
	private double [] nums2 = null;

	/**
	 * This method runs before any check
	 */
	@Before
	public void antesQue(){
		c1 = new NumComplejo(1,0);
	}

	/**
	 * Tests the "sumarComplejos" method
	 */
	@Test
	public void sumaTest(){
		c1 = c1.sumarComplejos(c1, c2);
		assertEquals(2.0, c1.getpReal(),0);
		assertEquals(1.0, c1.getpImaginaria(),0);
	}

	/**
	 * Tests the "restarComplejos" method
	 */
	@Test
	public void restaTest() {
		c1 = c1.restarComplejos(c1, c2);
		assertEquals(0.0, c1.getpReal(),0);
		assertEquals(-1.0, c1.getpImaginaria(),0);
	}

	/**
	 * Tests the "multiplicarComplejos" method
	 */
	@Test
	public void multiplicaciónTest() {
		c1 = c1.multiplicarComplejos(c1, c2);
		System.out.println("Resultado de la multiplicación: "+c1.toString());
		assertEquals(1.0, c1.getpReal(),0);
		assertEquals(1.0, c1.getpImaginaria(),0);
	}

	/**
	 * Tests the "dividirComplejos" method
	 */
	@Test
	public void divisionTest() {
		c1 = c1.dividirComplejos(c1, c2);
		System.out.println("Resultado de la división: "+c1.toString());
		assertEquals(0.5, c1.getpReal(),0);
		assertEquals(-0.5, c1.getpImaginaria(),0);
	}

	/**
	 * Tests the "modulo" method
	 */
	@Test
	public void moduloTest() {
		double modulo = c2.modulo(c2);
		System.out.println("Modulo: "+modulo);
		assertEquals(Math.sqrt(2),c2.modulo(c2),0);

	}

	/**
	 * Checks whether two arrays are equal
	 */
	@Test
	public void array(){
		assertArrayEquals(nums, c1.getUnArray(c1), 0);
	}

	/**
	 * Checks whether the result is TRUE
	 */
	@Test
	public void esTrue(){
		assertTrue(c1.esPositivo(c1.getpReal()));
	}

	/**
	 * Checks whether the method result is FALSE
	 */
	@Test
	public void esFalse(){
		assertFalse(c1.esPositivo(c1.getpImaginaria()));
	}

	/**
	 * Checks whether the parameter is null
	 */
	@Test
	public void esNull(){
		assertNull(nums2);
	}

	/**
	 * Checks whether the objects are the same
	 */
	@Test
	public void theSame(){
		System.out.println("Array antes de modificarse: "+nums[0]+"  "+nums[1]);
		c1.getOtroArray(nums);
		assertSame(nums, nums);
		System.out.println("Array despues de modificarse: "+nums[0]+"  "+nums[1]);
	}

	/**
	 * Checks whether the objects are different
	 */
	@Test
	public void noTheSame(){
		assertNotSame(c1, c2);
	}

	/**
	 * Checks that the method takes less than 5 seconds to run
	 * @throws InterruptedException
	 */
	@Test (timeout=5000)
	public void time () throws InterruptedException{
		int i = c1.time();
	}

}

“Introduction to JUnit (Video)” by Ricardo Moya at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

Ricardo Moya

I work in Machine Learning and Big Data. I like learning new technologies and explaining them as clearly as I can on this site.

You may also like

Comments

Leave a comment


This site uses Akismet to reduce spam. Learn how your comment data is processed.