This blog discusses the basics of JUnit. Ok, let's start with understanding what a framework is?
What is a framework
A framework is a predefined structure that dictates the structure and flow of control of our program. We create our application on top of a framework.
We can also say that a framework is a large collection of pre-written code, to which we add our code to solve a problem or carry out some functionality. Examples of a framework - JUnit, Java Swings, AWT classes etc.
Having seen what a framework is, we can now define JUnit.
What is JUnit
JUnit is a unit testing framework for Java programming language. It is a member of the unit testing framework family, collectively known as xUnit.
We have just seen that a framework is a predefined structure. As a result to be able to use JUnit in our application / program we have to follow the rules of JUnit. We have to follow its predefined annotations to carry out work.
Annotations in JUnit
List of commonly used annotations are:
1) @Test
2) @Ignore
3) @Before
4) @After
5) @BeforeClass
6) @AfterClass
7) @RunWith(Suite.class)
8) @SuiteClasses({,})
9) @Parameters
10) @RunWith(Parameterized.class)
Let's quickly see what these annotations are and what purpose do they serve:
@Test : This annotation depicts a test case. All the methods that are preceded by this annotation are actually test cases.
@Ignore : As the name suggests, this annotation is used when we want to ignore a particular test case. Example:
@Ignore
@Test
public void testFreeTrialSignup()
{
//test case code here..
}
Now when you execute the file containing the above test case as a JUnit test, this test case will be skipped.
@Before : Method preceded by this annotation will be called once before executing each test case. So if you have 4 test cases in your java file, the @Before annotation method will execute 4 times.
@After : Just like the @Before annotation, method preceded by @After annotation will be called once after executing each test case.
@BeforeClass : Method preceded by this annotation will be called once before executing all test cases in a java file. So if you have 4 test cases in your java file, the @BeforeClass annotation method will execute only once.
@AfterClass : Method preceded by this annotation will be called once after executing all test cases in a java file. So if you have 4 test cases in your java file, the @AfterClass annotation method will execute only once.
@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class})
@Parameters
@RunWith(Parameterized.class)
Thanks :)
What is a framework
A framework is a predefined structure that dictates the structure and flow of control of our program. We create our application on top of a framework.
We can also say that a framework is a large collection of pre-written code, to which we add our code to solve a problem or carry out some functionality. Examples of a framework - JUnit, Java Swings, AWT classes etc.
Having seen what a framework is, we can now define JUnit.
What is JUnit
JUnit is a unit testing framework for Java programming language. It is a member of the unit testing framework family, collectively known as xUnit.
We have just seen that a framework is a predefined structure. As a result to be able to use JUnit in our application / program we have to follow the rules of JUnit. We have to follow its predefined annotations to carry out work.
Annotations in JUnit
List of commonly used annotations are:
1) @Test
2) @Ignore
3) @Before
4) @After
5) @BeforeClass
6) @AfterClass
7) @RunWith(Suite.class)
8) @SuiteClasses({,})
9) @Parameters
10) @RunWith(Parameterized.class)
Let's quickly see what these annotations are and what purpose do they serve:
@Test : This annotation depicts a test case. All the methods that are preceded by this annotation are actually test cases.
@Ignore : As the name suggests, this annotation is used when we want to ignore a particular test case. Example:
@Ignore
@Test
public void testFreeTrialSignup()
{
//test case code here..
}
Now when you execute the file containing the above test case as a JUnit test, this test case will be skipped.
@Before : Method preceded by this annotation will be called once before executing each test case. So if you have 4 test cases in your java file, the @Before annotation method will execute 4 times.
@After : Just like the @Before annotation, method preceded by @After annotation will be called once after executing each test case.
@BeforeClass : Method preceded by this annotation will be called once before executing all test cases in a java file. So if you have 4 test cases in your java file, the @BeforeClass annotation method will execute only once.
@AfterClass : Method preceded by this annotation will be called once after executing all test cases in a java file. So if you have 4 test cases in your java file, the @AfterClass annotation method will execute only once.
@RunWith(Suite.class)
@SuiteClasses({Test1.class, Test2.class})
Let say in your program, you have 3 java files that contains all the test cases. You can execute each file invidually as a JUnit test. If you wish to execute all your test cases in a single go, you will have to execute them as a part of the test suite with the help of the above two annotations. Sample example:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({TestFreeSignUp.class,TestLogin.class,TestNewEvent.class})
public class TestSuite
{
}
@RunWith(Parameterized.class)
The above two annotations are used when ever you want to create parameterized test cases.
Note that the methods preceded by @BeforeClass, @AfterClass & @Parameters must be public and static methods. This is by rule. If you skip any or both the keywords, eclipse will not complain but as you try to run the test suite, JUnit will throw errors.
Methods preceded by all other annotations listed above must be public methods.
A sweet and simple program that shows the use of some of the annotations listed above is as follows. Copy paste in your eclipse project to see output:
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class testJUnitAnnotations
{
@BeforeClass
public static void atTheRateBeforeClassAnnotationMethod()
{
System.out.println("I am @BeforeClass preceded method. I will run only once." +
" Use me to do initializations.");
}
@Before
public void atTheRateBeforeAnnotationMethod()
{
System.out.println("I am @Before preceded method. I will run each time" +
" before a test is executed.");
}
@After
public void atTheRateAfterAnnotationMethod()
{
System.out.println("I am @After preceded method. I will run each time" +
" after a test case completes execution.");
}
@Test
public void atTheRateTestMehtod1()
{
System.out.println("I am @Test preceded method. I am a test case.");
}
@Ignore
@Test
public void atTheRateIgnoredTestMehtod()
{
System.out.println("I am @Test preceded method. I am an ignored test case.");
}
@Test
public void atTheRateTestMehtod2()
{
System.out.println("I am @Test preceded method. I am a test case.");
}
@AfterClass
public static void atTheRateAfterClassAnnotationMethod()
{
System.out.println("I am @AfterClass preceded method. I will run only once." +
" Use me to do initializations.");
}
}
The output of the program is shown below:
This sums up the JUnit basics tutuorial. Comments are welcome.Note that the methods preceded by @BeforeClass, @AfterClass & @Parameters must be public and static methods. This is by rule. If you skip any or both the keywords, eclipse will not complain but as you try to run the test suite, JUnit will throw errors.
Methods preceded by all other annotations listed above must be public methods.
A sweet and simple program that shows the use of some of the annotations listed above is as follows. Copy paste in your eclipse project to see output:
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class testJUnitAnnotations
{
@BeforeClass
public static void atTheRateBeforeClassAnnotationMethod()
{
System.out.println("I am @BeforeClass preceded method. I will run only once." +
" Use me to do initializations.");
}
@Before
public void atTheRateBeforeAnnotationMethod()
{
System.out.println("I am @Before preceded method. I will run each time" +
" before a test is executed.");
}
@After
public void atTheRateAfterAnnotationMethod()
{
System.out.println("I am @After preceded method. I will run each time" +
" after a test case completes execution.");
}
@Test
public void atTheRateTestMehtod1()
{
System.out.println("I am @Test preceded method. I am a test case.");
}
@Ignore
@Test
public void atTheRateIgnoredTestMehtod()
{
System.out.println("I am @Test preceded method. I am an ignored test case.");
}
@Test
public void atTheRateTestMehtod2()
{
System.out.println("I am @Test preceded method. I am a test case.");
}
@AfterClass
public static void atTheRateAfterClassAnnotationMethod()
{
System.out.println("I am @AfterClass preceded method. I will run only once." +
" Use me to do initializations.");
}
}
The output of the program is shown below:
Thanks :)