This is one of the post in series of simple Junit tutorials. I loved the book Junit in Action so lots of content is inspired by that book.
I will be explaining with simple example and also from time to time pointing you to read certain pages from API of Junit , so that you can have solid foundation of junit.
Junit is used for unit testing of code we write. When we say unit testing we mean testing as simple as one simple method to see if its is playing as per our expectations.
I will write one simple Calculator Class which has one simple method to add two numbers. We will write junit test to see if our method is implemented in correct way or not.Although this can seem to you , its foolish to test the addition of two numbers , but motive here is to understand the working of junit.
Before going through the tutorials you would like to setup your development environment for the tutorials. Read the previous post on setting up junit while using Ant task
package com.learnjunit;
public class Calculator {
public double add(double number1, double number2) {
return number1 + number2;
}
}
We have one simple Calculator class and one method add which is taking two numbers and returning their sum
Now we would write test case for this class
Best Practice : Name the test class as class with same name as class being tested + word Test so the test class in our case would CalculatorTest.java
package com.learnjunit.test;
import com.learnjunit.*;
import org.junit.Test;
import static org.junit.Assert.*;
public class CalculatorTest {
/**
* Test annotation tells that this method can be tested by junit It can also
* accept two things
*
* @Test(expected=IndexOutOfBoundsException.class)
* @Test(timeout=100)
*/
@Test
public void testadd() {
Calculator c = new Calculator();
//Asserts that two doubles or floats are equal to within a positive delta.
double result=c.add(10,20);
// Check if result is equal to expected value of 30
// IF they are not equal , exception is throws with given message
assertEquals("Checking add method failed",30,result, 0);
}
}
The line by line explanation for the CalculatorTest.java is as follows
Line 1
We created this class in separate package com.learnjuit.test , its always good practice to keep test code separate from code being tested
Line 3
We are importing the package of class which is being tested ( in this case Calculator.java )
Line 17
We created one method named testadd() which is having annotation @Test , this annotation tells that this method has to be taken up by junit
We created one Calculator object then calculated result based on implementation of add method in Calculator.java
We used assertEquals method to verify if the result is same as the expected one.
Things to read from Junit API
Assert ( See various assert Methods)
If you have read the previous tutorial on setting up Ant Junit task then you can run it from command prompt
Go to folder where the build.xml file is
ant junit
The complete test report would be published in the target/test-report folder
You can download the complete source code from this link