Unit Testing with JUnit

This article describes basic test writing using JUnit.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml



Create following class

package com.kruders.bean;

public class Calculator {
	private int a;
	private int b;
	public int getA() {
		return a;
	public void setA(int a) {
		this.a = a;
	public int getB() {
		return b;
	public void setB(int b) {
		this.b = b;

2. JUnit Class

Create following class

package com.kruders.junit;

import org.junit.Test;
import com.kruders.bean.Calculator;
import static org.junit.Assert.assertEquals;

public class CalculatorTest{
	public void testAdd(){
		Calculator calc = new Calculator();
		assertEquals(14, calc.getA() + calc.getB());

3. Testing
Now when you run the project, following screen will be displayed if test case is successful.

Figure 1.1 Figure 1.1

If test case fails, following screen will be displayed.

Figure 1.2 Figure 1.2

You can download the source code of this example here.

No comments yet.

Leave a Reply