0% found this document useful (0 votes)
103 views68 pages

Lab 4 All

Unit testing involves testing individual units or components of software code to validate that each unit performs as expected. It is typically done during development by writing automated test cases for functions, methods and objects. The key benefits of unit testing include finding bugs early, maintaining code quality, and serving as documentation for the code base. It is considered a best practice for software development and is commonly performed using unit testing frameworks like JUnit for Java.

Uploaded by

KamyPretty S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views68 pages

Lab 4 All

Unit testing involves testing individual units or components of software code to validate that each unit performs as expected. It is typically done during development by writing automated test cases for functions, methods and objects. The key benefits of unit testing include finding bugs early, maintaining code quality, and serving as documentation for the code base. It is considered a best practice for software development and is commonly performed using unit testing frameworks like JUnit for Java.

Uploaded by

KamyPretty S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 68

What is Unit Testing?

UNIT TESTING is a type of software testing where individual units or components


of a software are tested. The purpose is to validate that each unit of the software
code performs as expected. Unit Testing is done during the development (coding
phase) of an application by the developers. Unit Tests isolate a section of code and
verify its correctness. A unit may be an individual function, method, procedure,
module, or object.
In SDLC, STLC, V Model, Unit testing is first level of testing done before integration
testing. Unit testing is a WhiteBox testing technique that is usually performed by
the developer. Though, in a practical world due to time crunch or reluctance of
developers to tests, QA engineers also do unit testing.

Why Unit Testing?


Unit Testing is important because software developers sometimes try saving time
doing minimal unit testing and this is myth because inappropriate unit testing
leads to high cost Defect fixing during System Testing, Integration Testing and even
Beta Testing after application is built. If proper unit testing is done in early
development, then it saves time and money in the end.

Here, are the key reasons to perform unit testing in software engineering:

Unit
Testing Levels

1. Unit tests help to fix bugs early in the development cycle and save costs.
2. It helps the developers to understand the testing code base and enables
them to make changes quickly
3. Good unit tests serve as project documentation
4. Unit tests help with code re-use. Migrate both your code and your tests to
your new project. Tweak the code until the tests run again.

How to do Unit Testing


In order to do Unit Testing, developers write a section of code to test a specific function in
software application. Developers can also isolate this function to test more rigorously which
reveals unnecessary dependencies between function being tested and other units so the
dependencies can be eliminated. Developers generally use UnitTest framework to develop
automated test cases for unit testing.
Unit Testing is of two types

 Manual
 Automated

Unit testing is commonly automated but may still be performed manually. Software Engineering
does not favor one over the other but automation is preferred. A manual approach to unit testing
may employ a step-by-step instructional document.

Under the automated approach-

 A developer writes a section of code in the application just to test the function. They
would later comment out and finally remove the test code when the application is
deployed.
 A developer could also isolate the function to test it more rigorously. This is a more
thorough unit testing practice that involves copy and paste of code to its own testing
environment than its natural environment. Isolating the code helps in revealing
unnecessary dependencies between the code being tested and other units or data
spaces in the product. These dependencies can then be eliminated.
 A coder generally uses a UnitTest Framework to develop automated test cases. Using an
automation framework, the developer codes criteria into the test to verify the correctness
of the code. During execution of the test cases, the framework logs failing test cases.
Many frameworks will also automatically flag and report, in summary, these failed test
cases. Depending on the severity of a failure, the framework may halt subsequent testing.
 The workflow of Unit Testing is 1) Create Test Cases 2) Review/Rework 3) Baseline 4)
Execute Test Cases.

Unit Testing Techniques


The Unit Testing Techniques are mainly categorized into three parts which are
Black box testing that involves testing of user interface along with input and
output, White box testing that involves testing the functional behaviour of the
software application and Gray box testing that is used to execute test suites, test
methods, test cases and performing risk analysis.
Code coverage techniques used in Unit Testing are listed below:

 Statement Coverage
 Decision Coverage
 Branch Coverage
 Condition Coverage
 Finite State Machine Coverage

Unit Test Example: Mock Objects


Unit testing relies on mock objects being created to test sections of code that are
not yet part of a complete application. Mock objects fill in for the missing parts of
the program.

For example, you might have a function that needs variables or objects that are not
created yet. In unit testing, those will be accounted for in the form of mock objects
created solely for the purpose of the unit testing done on that section of code.

Unit Testing Tools


There are several automated unit test software available to assist with unit testing.
We will provide a few examples below:

1. Junit: Junit is a free to use testing tool used for Java programming language.
It provides assertions to identify test method. This tool test data first and
then inserted in the piece of code.
2. NUnit:  NUnit is widely used unit-testing framework use for all .net
languages.  It is an open source tool which allows writing scripts manually. It
supports data-driven tests which can run in parallel.
3. JMockit:  JMockit is open source Unit testing tool.  It is a code coverage tool
with line and path metrics. It allows mocking API with recording and
verification syntax. This tool offers Line coverage, Path Coverage, and Data
Coverage.
4. EMMA:  EMMA is an open-source toolkit for analyzing and reporting code
written in Java language. Emma support coverage types like method, line,
basic block. It is Java-based so it is without external library dependencies
and can access the source code.
5. PHPUnit: PHPUnit is a unit testing tool for PHP programmer. It takes small
portions of code which is called units and test each of them separately.  The
tool also allows developers to use pre-define assertion methods to assert
that a system behave in a certain manner. 
Those are just a few of the available unit testing tools. There are lots more,
especially for C languages and Java, but you are sure to find a unit testing tool for
your programming needs regardless of the language you use.

Test Driven Development (TDD) & Unit Testing


Unit testing in TDD involves an extensive use of testing frameworks. A unit test
framework is used in order to create automated unit tests. Unit testing frameworks
are not unique to TDD, but they are essential to it. Below we look at some of what
TDD brings to the world of unit testing:

 Tests are written before the code


 Rely heavily on testing frameworks
 All classes in the applications are tested
 Quick and easy integration is made possible

Unit Testing Myth


Myth: It requires time, and I am always overscheduled
My code is rock solid! I do not need unit tests.

Myths by their very nature are false assumptions. These assumptions lead to a
vicious cycle as follows –
Truth is Unit testing increase the speed of development.

Programmers think that Integration Testing will catch all errors and do not execute
the unit test. Once units are integrated, very simple errors which could have very
easily found and fixed in unit tested take a very long time to be traced and fixed.

Unit Testing Advantage


 Developers looking to learn what functionality is provided by a unit and how
to use it can look at the unit tests to gain a basic understanding of the unit
API.
 Unit testing allows the programmer to refactor code at a later date, and
make sure the module still works correctly (i.e. Regression testing). The
procedure is to write test cases for all functions and methods so that
whenever a change causes a fault, it can be quickly identified and fixed.
 Due to the modular nature of the unit testing, we can test parts of the project
without waiting for others to be completed.

Unit Testing Disadvantages


 Unit testing can’t be expected to catch every error in a program. It is not
possible to evaluate all execution paths even in the most trivial programs
 Unit testing by its very nature focuses on a unit of code. Hence it can’t catch
integration errors or broad system level errors.

It’s recommended unit testing be used in conjunction with other testing activities.

Unit Testing Best Practices


 Unit Test cases should be independent. In case of any enhancements or
change in requirements, unit test cases should not be affected.
 Test only one code at a time.
 Follow clear and consistent naming conventions for your unit tests
 In case of a change in code in any module, ensure there is a corresponding
unit Test Case for the module, and the module passes the tests before
changing the implementation
 Bugs identified during unit testing must be fixed before proceeding to the
next phase in SDLC
 Adopt a “test as your code” approach. The more code you write without
testing, the more paths you have to check for errors.

Summary
 UNIT TESTING is defined as a type of software testing where individual units
or components of a software are tested.
 As you can see, there can be a lot involved in unit testing. It can be complex
or rather simple depending on the application being tested and the testing
strategies, tools and philosophies used. Unit testing is always necessary on
some level. That is a certainty.
Lesson 1: How to Download and Install JUnit in
Eclipse
PART 1) Install Java
JUnit is a Testing framework used to test Java based application. So before
installing JUnit, you need to configure or verify java development kit (JDK) in your
machine.

Click this tutorial to download and install Java

PART 2) Download JUnit


Step 1) Visit https://junit.org/junit4/ and click Download and Install

Step 2) Click junit.jar


Step 3) In the central repository you are shown all versions of Junit that can be
downloaded. Usually, you will select the latest version. Click on jar link to
download Junit version 4.12 as shown below

Step 4) Visit https://github.com/junit-team/junit4/wiki/Download-and-
Install again. Click hamcrest-core.jar
Step 5) Download the Jar

For JUnit installation, you need JUnit jars, and you can download the desired
version of JUnit jar file from JUnit official youbsite http://www.junit.org

Here is the jars list:

 JUnit.jar
 hamcrest-core.jar

PART 3) JUnit Environment Setup


Step 1) You need to set JUNIT_HOME environment variable to point out the base
location where you have placed JUnit Jars.

For example, if you have created a JUnit folder in c: drive and placed jars there,
then for environment settings you need to open control panel ->advanced -
>environment variable.

1. Under environment window clicks on “new” button.


When you click on new button in environment variables, it will open another
window

Step 2) A “New System Variable” window will open:

1. Provide variable name as “JUNIT_HOME”.


2. Provide JUnit value as JUnit path where you have copied JUnit jar files.
3. Click on OK.

When you click on OK, it will create a new system variable with the given name and
value. Which you can verify in environment variable window as shown in step 1
image.

Step 3) After creating JUNIT_HOME, create another variable with the name
CLASSPATH. Again go to Environment Variables and follow the below steps.
1. Click on “new” button. When you click on new in environment variables, it
will open another window.

Step 4) In this step, point out JUNIT_HOME to JUnit.jar which is placed in JUnit
folder as given below:

1. Variable Name: CLASSPATH


2. Variable Value: %CLASSPATH%;%JUNIT_HOME%\JUnit4.10.jar;.;
3. Click on the OK button.

Step 5) Once you click on the ‘OK’ button, you can verify that a new environment
variable named “CLASSPATH” can be seen under system variable. See below
PART 4) Install JUnit jar file in eclipse
Step 1) Right click on project:

1. Click on “build path” and then


2. Click on “Configure build path”.

Step 2) In this step,

1. Go to java build path window as shown in below figure


2. Now click on “Add External JARs” button to add your downloaded JUnit.jar
file with eclipse.

After adding a JUnit.jar file, click on ‘OK’ button to close java build path window.
Part 5) Verifying whether required jar file for JUnit is
in my build path
In order to verify JUnit jar file in eclipse, you need to follow below-mentioned
steps:

1. Right click on project -> Build Path


2. Click on “Configure build path”.

Step 2) Java build path window will appear as shown below.

In that window, go to Libraries tab to see all jar files. In jar file tree view, you need
to look for the jar file name which is starting with JUnit.

Once you expand JUnit libraries, you can see java libraries as shown below:
Now you are ready to use JUnit with eclipse.

PART 6) Verify JUnit setup


You can create a simple JUnit test to verify JUnit setup. See below test class:

Step 1) Create a java class named TestJUnit.java and provide a simple assert
statement.

package guru99.junit;

import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {
@Test
public void testSetup() {
String str= "I am done with Junit setup";
assertEquals("I am done with Junit setup",str);
}
}
Step 2) Create a Test Runner class to execute above test.
package guru99.junit;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
Step 3) To execute the test, follow below steps:

1. Right click on TestJunit.java and click on “Run As” as shown below


2. Another window will be open once you click on “Run As”, click on “1 JUnit
Test” as shown below:
Step 4) Here is the output or result for your test. If it is successfully executed, it will
show a green check mark in front of it.
Click to download the above code for your testing purposes.

Lesson 2: JUnit Test Cases @Before


@BeforeClass Annotation
JUnit is the most popular unit Testing framework in Java. It is explicitly
recommended for Unit Testing. JUnit does not require server for testing web
application, which makes the testing process fast.

JUnit framework also allows quick and easy generation of test cases and test data.
The org.Junit package consist of many interfaces and classes for JUnit Testing
such as Test, Assert, After, Before, etc.

What is Test fixture


Before we understand what a test fixture is, let’s study the code below

This code is designed to execute two test cases on a simple file.

public class OutputFileTest {


private File output;
output = new File(...);
output.delete();
public void testFile1(){
//Code to verify Test Case 1
}
output.delete();
output = new File(...);
public void testFile2(){
//Code to verify Test Case 2
}
output.delete();
}
Few issues here

 The code is not readable


 The code is not easy to maintain.
 When the test suite is complex the code could contain logical issues.

Compare the same code using JUnit

public class OutputFileTest


{
private File output;
@Before public void createOutputFile()
{
output = new File(...);
}

@After public void deleteOutputFile()


{
output.delete();
}

@Test public void testFile1()


{
// code for test case objective
}
@Test public void testFile2()
{
// code for test case objective
}
}
The code far more readable and maintainable. The above code structure is a Test
fixture.

A test fixture is a context where a JUnit Test Case runs. Typically, test fixtures


include:

 Objects or resources that are available for any test case.


 Activities required that makes these objects/resources available.
 These activities are
1. allocation (setup)
2. de-allocation (teardown).

Setup and Teardown


 Usually, there are some repeated tasks that must be done prior to each test
case. Example:
create a database connection.
 Likewise, at the end of each test case, there may be some repeated
tasks. Example: to clean up once test execution is over.
 JUnit provides annotations that help in setup and teardown. It ensures that
resources are released, and the test system is in a ready state for next test
case.

These JUnit annotations are discussed below-

Setup
@Before annotation in JUnit is used on a method containing Java code to run
before each test case. i.e it runs before each test execution.

Teardown (regardless of the verdict)


@After annotation is used on a method containing java code to run after each test
case. These methods will run even if any exceptions are thrown in the test case or
in the case of assertion failures.

Note:

 It is allowed to have any number of annotations listed above.


 All the methods annotated with @Before in JUnit will run before each test
case, but they may run in any order.
 You can inherit @Before and @After methods from a super class, Execution
is as follows: It is a standard execution process in JUnit.

1. Execute the JUnit @Before methods in the superclass


2. Execute the @Before methods in this class
3. Execute a @Test method in this class
4. Execute the @After methods in this class
5. Execute the @After methods in the superclass

Example: Creating a class with file as a test fixture

public class OutputFileTest


{
private File output;
@Before public void createOutputFile()
{
output = new File(...);
}

@After public void deleteOutputFile()


{
output.delete();
}

@Test public void testFile1()


{
// code for test case objective
}
@Test public void testFile2()
{
// code for test case objective
}
}
In the above example the chain of execution will be as follows-

1. createOutputFile()
2. testFile1()
3. deleteOutputFile()
4. createOutputFile()
5. testFile2()
6. deleteOutputFile()

Assumption:
testFile1() runs before testFile2()– which is not guaranteed.

Once-only setup
 It is possible to run a method only once for the entire test class before any of
the tests are executed, and prior to any @Before method(s).
 “Once only setup” are useful for starting servers, opening communications,
etc. It’s time-consuming to close and re-open resources for each test.
 This can be done using the annotation @BeforeClass in JUnit.

@BeforeClass public static void Method_Name() {


// class setup code here
}
Once-only tear down
 Similar to once only setup , a once-only cleanup method is also available. It
runs after all test case methods and @After annotations have been
executed.
 It is useful for stopping servers, closing communication links, etc.
 This can be done using the @AfterClass annotation

@AfterClass public static void Method_Name()


{
// class cleanup code here
}

JUnit Test Suites


If we want to execute multiple tests in a specified order, it can be done by
combining all the tests in one place. This place is called as the test suites. More
details on how to execute test suites and how it is used in JUnit will be covered in
this tutorial.

Junit Test Runner


JUnit provides a tool for execution of your test cases.

 JUnitCore class is used to execute these tests.


 A method called runClasses provided by org.junit.runner.JUnitCore, is
used to run one or several test classes.
 Return type of this method is the Result object (org.junit.runner.Result),
which is used to access information about the tests. See following code
example for more clarity.

public class Test {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(CreateAndSetName.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
In above code “result” object is processed to get failures and successful outcomes
of test cases we are executing.

First JUnit program


Fair knowledge of SDLC, java programming, and basics of software testing process
helps in understanding JUnit program.
Let’s understand Unit Testing using a live example. We need to create a test class
with a test method annotated with @Test as given below:

MyFirstClassTest.java

package guru99.JUnit;

import static org.JUnit.Assert.*;

import org.JUnit.Test;

public class MyFirstClassTest {

@Test
public void myFirstMethod(){
String str= "JUnit is working fine";
assertEquals("JUnit is working fine",str);
}
}
TestRunner.java

To execute our test method (above) ,we need to create a test runner. In the test
runner we have to add test class as a parameter in JUnitCore’s runclasses() method
. It will return the test result, based on whether the test is passed or failed.

For more details on this see the code below :

package guru99.JUnit;

import org.JUnit.runner.JUnitCore;
import org.JUnit.runner.Result;
import org.JUnit.runner.notification.Failure;

public class TestRunner {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(MyFirstClassTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
Output

Once TestRunner.java executes our test methods we get output as failed or


passed. Please find below output explanation:

1. In this example, after executing MyFirstClassTest.java , test is passed and


result is in green.
2. If it would have failed it should have shown the result as Red and failure can
be observed in failure trace. See below JUnit gui :
Summary
 JUnit is a framework which supports several annotations to identify a
method which contains a test.
 JUnit provides an annotation called @Test, which tells the JUnit that the
public void method in which it is used can run as a test case.
 A test fixture is a context where a test case runs
 To execute multiple tests in a specified order, it can be done by combining
all the tests in one place. This place is called as the test suites.
 JUnit provides a tool for execution of the tests where we can run our test
cases referred as Test Runner.

Lesson 3: JUnit Annotations Tutorial with


Example: What is @Test and @After
What is JUnit Annotations?
JUnit Annotations is a special form of syntactic meta-data that can be added to
Java source code for better code readability and structure. Variables, parameters,
packages, methods and classes can be annotated. Annotations were introduced in
Junit4, which makes Java code more readable and simple. This is the big difference
between Junit3 and Junit4 that Junit4 is annotation based.
With the knowledge of annotations in Junit5, one can easily learn and implement a
JUnit test. Below is the important and frequently used JUnit annotations list:
S.No. Annotations Description

This annotation is a replacement of org.junit.TestCase which


1. @Test indicates that public void method to which it is attached can be
executed as a test Case.

This annotation is used if you want to execute some statement


2. @Before
such as preconditions before each test case.

This annotation is used if you want to execute some statements


3. @BeforeClass before all the test cases for e.g. test connection must be executed
before all the test cases.

This annotation can be used if you want to execute some


4. @After statements after each Test Case for e.g resetting variables,
deleting temporary files ,variables, etc.

This annotation can be used if you want to execute some


5. @AfterClass statements after all test cases for e.g. Releasing resources after
executing all test cases.

This annotation can be used if you want to ignore some


6. @Ignores statements during test execution for e.g. disabling some test
cases during test execution.

This annotation can be used if you want to set some timeout


during test execution for e.g. if you are working under some SLA
7. @Test(timeout=500)
(Service level agreement), and tests need to be completed within
some specified time.

This annotation can be used if you want to handle some


@Test(expected=Illegal
exception during test execution. For, e.g., if you want to check
8. ArgumentException.cla
whether a particular method is throwing specified exception or
ss)
not.

JUnit Annotations Example


Let’s create a class covering important JUnit annotations with simple print
statements and execute it with a test runner class:

Step 1) Consider below java class having various methods which are attached to
above-listed annotations:
JunitAnnotationsExample.java
package guru99.junit;

import static org.junit.Assert.assertEquals;


import static org.junit.Assert.assertFalse;

import java.util.ArrayList;

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 JunitAnnotationsExample {

private ArrayList<String> list;

@BeforeClass
public static void m1() {
System.out.println("Using @BeforeClass , executed before all test cases ");
}

@Before
public void m2() {
list = new ArrayList<String>();
System.out.println("Using @Before annotations ,executed before each test cases ");
}

@AfterClass
public static void m3() {
System.out.println("Using @AfterClass ,executed after all test cases");
}

@After
public void m4() {
list.clear();
System.out.println("Using @After ,executed after each test cases");
}

@Test
public void m5() {
list.add("test");
assertFalse(list.isEmpty());
assertEquals(1, list.size());
}

@Ignore
public void m6() {
System.out.println("Using @Ignore , this execution is ignored");
}

@Test(timeout = 10)
public void m7() {
System.out.println("Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case");
}

@Test(expected = NoSuchMethodException.class)
public void m8() {
System.out.println("Using @Test(expected) ,it will check for specified exception during its execution");
}

}
Step 2) let’s create a test runner class to execute above test:

TestRunner.java
package guru99.junit;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitAnnotationsExample.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
Expected Result
 All the test cases will be executed one by one, and all print statement can be
seen on a console.
 As discussed in above table @Before annotation in JUnit, @BeforeClass
[ method m1() and m2() ] will be executed before each and before all test
cases respectively.
 In the same way @After in JUnit, @afterClass (method m3() and m4()) will be
executed after each and after all test cases respectively. @ignore (method
m6())will be treated as ignoring the test.

Let’s analyse test cases used in above java class in detail:

1. Consider method m5() as given below :

@Test
public void m5() {
list.add("test");
assertFalse(list.isEmpty());
assertEquals(1, list.size());
}
In above method as you are adding a string in the variable “list” so

 list.isEmpty() will return false.


 assertFalse(list.isEmpty()) must return true.
 As a result, the test case will pass.

As you have added only one string in the list, so the size is one.
 list.size() must return int value as “1” .
 So assertEquals(1, list.size()) must return true.
 As a result, the test case will pass.

2. Consider method m7() as given below :

@Test(timeout = 10)
public void m7() {
System.out.println("Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case");
}
As discussed above @Test(timeout = 10)annotation is used to enforce timeout in
the test case.

3. Consider method m8() as given below :

@Test(expected = NoSuchMethodException.class)
public void m8() {
System.out.println("Using @Test(expected) ,it will check for specified exception during its execution");

}
As discussed above @Test(expected) will check for specified exception during its
execution so method m8() will throw “No Such Method Exception.” As a result, the
test will be executed with an exception.

As all test cases are passed, this results in a successful test execution.

Actual Result
As there are three test cases in above example, all test cases will be executed one
by one. See output below:

JUnit Annotations Example


See below print statements which can be seen on console:

Using @BeforeClass , executed before all test cases

Using @Before annotations, executed before each test cases

Using @After, executed after each test cases


Using @Before annotations, executed before each test cases

Using @Test(timeout),it can be used to enforce timeout in JUnit4 test case

Using @After, executed after each test cases

Using @Before annotations, executed before each test cases

Using @Test(expected) ,it will check for specified exception during its execution

Using @After, executed after each test cases

Using @AfterClass, executed after all test cases

JUnit Assert Class


This class provides a bunch of assertion methods useful in writing a test case. If all
assert statements are passed, test results are successful. If any assert statement
fails, test results are failed.

As you seen earlier, below table describes important Assert methods and
description:

S.No. Method Description

void assertEquals(boolean It checks whether two values are equals similar to


1.
expected, boolean actual) equals method of Object class

2. void assertFalse(boolean condition) functionality is to check that a condition is false.

“assertNotNull” functionality is to check that an


3. void assertNotNull(Object object)
object is not null.

“assertNull” functionality is to check that an object


4. void assertNull(Object object)
is null.

“assertTrue” functionality is to check that a


5. void assertTrue(boolean condition)
condition is true.

If you want to throw any assertion error, you have


6. void fail()
fail() that always results in a fail verdict.

“assertSame” functionality is to check that the two


7. void assertSame([String message]
objects refer to the same object.

8. void assertNotSame([String “assertNotSame” functionality is to check that the


S.No. Method Description

message] two objects do not refer to the same object.

JUnit Test Cases Class


To run multiple test, TestCase class is available in org.junit.TestCase packages.
@Test annotation tells JUnit that this public void method (Test Case here) to which
it is attached can be run as a test case.

Below table shows some important methods available in org.junit.TestCase class:

S.No. Method Description

This method is used to count how many number of test cases


1. int countTestCases()
executed by run(TestResult tr) method.

TestResult
2. This method is used to create a TestResult object.
createResult()

3. String getName() This method returns a string which is nothing but a TestCase.

This method is used to execute a test which returns


4. TestResult run()
a TestResult object

void run(TestResult This method is used to execute a test having a TestResult obje


5.
result) which doesn’t returns anything.

void setName(String
6. This method is used to set a name of a TestCase.
name)

This method is used to write resource association code. e.g. Cre


7. void setUp()
a database connection.

This method is used to write resource release code. e.g. Release


8. void tearDown()
database connection after performing transaction operation.

JUnit TestResult Class


When you execute a test, it returns a result (in the form of TestResult object). This
TestResult object can be used to analyse the resultant object. This test result can
be either failure or successful.
See below table for important methods used in org.junit.TestResult class:
S.No. Method Description

This method is used if you require add an error to


1. void addError(Test test, Throwable t)
the test.

void addFailure(Test test, This method is used if you require add a failure to
2.
AssertionFailedError t) the list of failures.

This method is used to notify that a test is


3. void endTest(Test test)
performed(completed)

This method is used to get the error detected


4. int errorCount()
during test execution.

This method simply returns a collection


5. Enumeration<TestFailure> errors()
(Enumeration here) of errors.

This method is used to get the count of errors


6. int failureCount()
detected during test execution.

7. void run(TestCase test) This method is used to execute a test case.

8. int runCount() This method simply counts the executed test.

9. void startTest(Test test) This method is used to notify that a test is started.

10. void stop() This method is used to test run to be stopped.

JUnit Test Suite Class


If you want to execute multiple tests in a specified order, it can be done by
combining all the tests in one place. This place is called as the test suites.

See below table for important methods used in org.junit.TestSuite class:

S.No. Method Description

This method is used if you want to add a test to the


1. void addTest(Test test)
suite.

void addTestSuite(Class<? extends This method is used if you want to specify the class
2.
TestCase> testClass) while adding a test to the suite.

This method is used if you want to count the


3. int countTestCases()
number of test cases.
S.No. Method Description

This method is used to get the name of the test


4. String getName()
suite.

This method is used to execute a test and collect


5. void run(TestResult result)
test result in TestResult object.

6. void setName(String name) This method is used to set the name of TestSuite.

This method is used if you want to return the test


7. Test testAt(int index)
at given index.

This method is used if you want to return a number


8. int testCount()
of tests in the Suite.

This method returns a test which will fail and log a


9. static Test warning(String message)
warning message.

Summary:
 JUnit provides a portable API, which provides all important classes and
Selenium annotations useful in writing a unit test.
 Classes which are very useful while writing a test case
 org.junit.Assert
 org.junit.TestCase
 org.junit.TestResult
 org.junit.TestSuite
 Important and frequently used JUnit annotations list

@Before

@BeforeClass

@After

@AfterClass

@Test

@Ignore
Lesson 4: Junit Assert & AssertEquals with
Example
What is Junit Assert?
Assert is a method useful in determining Pass or Fail status of a test case, The
assert methods are provided by the class org.junit.Assert which extends
java.lang.Object class.

There are various types of assertions like Boolean, Null, Identical etc.

Junit provides a class named Assert, which provides a bunch of assertion methods
useful in writing test cases and to detect test failure

The assert methods are provided by the class org.junit.Assert which


extends java.lang.Object class.

JUnit Assert methods


Boolean
If you want to test the boolean conditions (true or false), you can use following
assert methods

1. assertTrue(condition)
2. assertFalse(condition)

Here the condition is a boolean value.

Null object
If you want to check the initial value of an object/variable, you have the following
methods:

1. assertNull(object)
2. assertNotNull(object)

Here object is Java object e.g. assertNull(actual);

Identical
If you want to check whether the objects are identical (i.e. comparing two
references to the same java object), or different.
1. assertSame(expected, actual), It will return true if expected == actual
2. assertNotSame(expected, actual)

Assert Equals
If you want to test equality of two objects, you have the following methods

 assertEquals(expected, actual)

It will return true if: expected.equals( actual ) returns true.

Assert Array Equals


If you want to test equality of arrays, you have the following methods as given
below:

 assertArrayEquals(expected, actual)

Above method must be used if arrays have the same length, for each valid value
for i, you can check it as given below:

 assertEquals(expected[i],actual[i])
 assertArrayEquals(expected[i],actual[i])

Fail Message
If you want to throw any assertion error, you have fail() that always results in a fail
verdict.

 Fail(message);

You can have assertion method with an additional String parameter as the first


parameter. This string will be appended in the failure message if the assertion fails.
E.g. fail( message ) can be written as

 assertEquals( message, expected, actual)

JUnit assertEquals
You have assertEquals(a,b) which relies on the equals() method of the Object
class.

 Here it will be evaluated as a.equals( b ).


 Here the class under test is used to determine a suitable equality relation.
 If a class does not override the equals() method of Object class, it will get
the default behaviour of equals() method, i.e. object identity.
If a and b are primitives such as byte, int, boolean, etc. then the following will be
done for assertEquals(a,b) :

a and b will be converted to their equivalent wrapper object type


(Byte,Integer, Boolean, etc.), and then a.equals( b ) will be evaluated.

For Example: Consider below-mentioned strings having same values, let’s test it
using assertTrue

String obj1="Junit";
String obj2="Junit";
assertEquals(obj1,obj2);
Above assert statement will return true as obj1.equals(obj2) returns true.

Floating point assertions


When you want to compare floating point types (e.g. double or float), you need an
additional required parameter delta to avoid problems with round-off errors while
doing floating point comparisons.

The assertion evaluates as given below:

 Math.abs( expected – actual ) <= delta

For example:

assertEquals( aDoubleValue, anotherDoubleValue, 0.001 )

JUnit Assert Example


Below example demonstrates how to assert a condition using JUnit assert
methods.

Let’s create a simple test class named Junit4AssertionTest.java and a test runner


class TestRunner.java.

You will create few variables and important assert statements in JUnit.

In this example, you will execute our test class using TestRunner.java

Step 1) Lets create a class covering all important assert statement methods in
junit:

Junit4AssertionTest.java

package guru99.junit;
import static org.junit.Assert.*;
import org.junit.Test;

public class Junit4AssertionTest {

@Test
public void testAssert(){

//Variable declaration
String string1="Junit";
String string2="Junit";
String string3="test";
String string4="test";
String string5=null;
int variable1=1;
int variable2=2;
int[] airethematicArrary1 = { 1, 2, 3 };
int[] airethematicArrary2 = { 1, 2, 3 };

//Assert statements
assertEquals(string1,string2);
assertSame(string3, string4);
assertNotSame(string1, string3);
assertNotNull(string1);
assertNull(string5);
assertTrue(variable1<variable2);
assertArrayEquals(airethematicArrary1, airethematicArrary2);
}
}
Step 2) You need to create a test runner class to execute above class:

TestRunner.java

package guru99.junit;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(Junit4AssertionTest.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
Step 3) Lets analyse expected output step by step:

Consider all assert statements one by one:

1. assertEquals(string1,string2);

Now compare string1=” Junit” with string2=” Junit” with equals method of object
class. Replacing assertEquals method from java.lang.Object.equals() method :
string1.equals(string2)=> returns true

So assertEquals(string1,string2) will return true.

2. assertSame(string3, string4);

“assertSame()” functionality is to check that the two objects refer to the same
object.

Since string3=”test” and string4=”test” means both string3 and string4 are of the
same type so assertSame(string3, string4) will return true.

3. assertNotSame(string1, string3);

“assertNotSame()” functionality is to check that the two objects do not refer to the
same object.

Since string1=”Junit” and string3=”test” means both string1 and string3 are of
different types, so assertNotSame(string1, string3) will return true.

4. assertNotNull(string1);

“assertNotNull()” functionality is to check that an object is not null.

Since string1= “Junit” which is a non-null value so assertNotNull(string1) will


return true.

5. assertNull(string5);

“assertNull()” functionality is to check that an object is null.

Since string5= null which is a null value so assertNull(string5) will return true.

6. assertTrue(variable1<variable2);

“assertTrue()” functionality is to check that a condition is true.

Since variable1=1 and variable2=2, which shows that variable1<variable2 condition


is true so assertTrue(variable1<variable2) will return true.

7. assertArrayEquals(airethematicArrary1, airethematicArrary2);

“assertArrayEquals()” functionality is to check that the expected array and the


resulted array are equal. The type of Array might be int, long, short, char, byte or
java.lang.Object.
Since airethematicArrary1 = { 1, 2, 3 } and airethematicArrary2 = { 1, 2, 3 } which
shows both the arrays are equal so assertArrayEquals(airethematicArrary1,
airethematicArrary2) will return true

Since all seven assert statements of Junit4AssertionTest.java class returns true,


therefore when you execute the test assert class, it will return a successful test.
(see the output below)

Step 4) Right click on Junit4AssertionTest.java and click on runAs->JUnit. You will


see the output as given below:

Above output shows a successful test result as expected.

Summary:
In this tutorial, you learned all important types of assertion methods provided by
JUnit. Also, you have seen the examples of assert statements. Which shows that if
all assert statements return true, then the test GUI will return a true result and if
the single test fails it will return a failed result.

Lesson 5: Create JUnit Test Suite with Example:


@RunWith @SuiteClasses
In Junit, test suite allows us to aggregate all test cases from multiple classes in one
place and run it together.

To run the suite test, you need to annotate a class using below-mentioned
annotations:

1. @Runwith(Suite.class)
2. @SuiteClasses(test1.class,test2.class……) or

@Suite.SuiteClasses ({test1.class, test2.class……})


With above annotations, all the test classes in the suite will start executing one by
one.
Steps to create Test Suite and Test Runner
Step 1) Create a simple test class (e.g. MyFirstClassTest) and add a method
annotated with @test.

Step 2) Create another test class to add (e.g. MySecondClassTest) and create a
method annotated with @test.

Step 3) To create a testSuite you need to first annotate the class with
@RunWith(Suite.class) and @SuiteClasses(class1.class2…..).

Step 4) Create a Test Runner class to run our test suite as given below;
Code Explanation:

 Code Line 8: Declaring the main method of the class test which will run our
JUnit test.
 Code Line 9: Executing test cases using JunitCore.runclasses which takes
the test class name as a parameter (In the example above, you are using
TestSuiteExample.class shown in step 3).
 Code Line 11: Processing the result using for loop and printing out failed
result.
 Code Line 13: Printing out the successful result.

Output: Here is the output which shows successful test with no failure trace as
given below:
JUnit Test Suite Example
Consider a more complex example

JunitTest.java

JunitTest.java is a simple class annotated


with @RunWith and @Suite annotations. You can list out number of .classes in the
suite as parameters as given below:

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

@RunWith(Suite.class)
@Suite.SuiteClasses({
SuiteTest1.class,
SuiteTest2.class,
})

public class JunitTest {


// This class remains empty, it is used only as a holder for the above annotations
}
SuiteTest1.java

SuiteTest1.java is a test class having a test method to print out a message as given
below. You will use this class as a suite in above mentioned class.

package guru99.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class SuiteTest1 {

public String message = "Saurabh";

JUnitMessage junitMessage = new JUnitMessage(message);

@Test(expected = ArithmeticException.class)
public void testJUnitMessage() {

System.out.println("Junit Message is printing ");


junitMessage.printMessage();

@Test
public void testJUnitHiMessage() {
message = "Hi!" + message;
System.out.println("Junit Hi Message is printing ");
assertEquals(message, junitMessage.printHiMessage());
System.out.println("Suite Test 2 is successful " + message);
}
}
SuiteTest2.java

SuiteTest2.java is another test class similar to SuiteTest1.java having a test


method to print out a message as given below. You will use this class as suite
in JunitTest.java.

package guru99.junit;

import org.junit.Assert;
import org.junit.Test;

public class SuiteTest2 {

@Test
public void createAndSetName() {

String expected = "Y";


String actual = "Y";

Assert.assertEquals(expected, actual);
System.out.println("Suite Test 1 is successful " + actual);
}

}
Output

After executing JunitTest.java which contains a suite


having test1.java and test2.java, you will get below output:
Summary
In this tutorial, you have learned basics of test harness and test suites in details
with an example.

 Test harness or automation Testing is a software or a collection of software,


which allows a user to test data with multiple inputs and control the
execution
 Test harness actually enables a test framework that does all the work of
executing tests using a test library and generating a test report
 In Junit, test suite allows us to aggregate all test cases of many classes in
one place and run it together.

Lesson 6: JUnit @Ignore Test Annotation with


Example
Sometimes you may require not to execute a method/code or Test Case because
coding is not done fully. For that particular test, JUnit
provides @Ignore annotation to skip the test.

What is JUnit @Ignore test annotation


The @Ignore test annotation is used to ignore particular tests or group of tests in
order to skip the build failure.
@Ignore annotation can be used in two scenarios as given below:

1. If you want to ignore a test method, use @Ignore along with @Test
annotation.
2. If you want to ignore all the tests of class, use @Ignore annotation at the
class level.

You can provide the reason for disabling a test in the optional parameter provided
by @Ignore annotation.

It will help other developers working on the same piece of code, to understand
“why a particular test is disabled?” When the issue of that particular test is fixed,
you can simply enable it by removing @Ignore annotation.

Junit Test Example – Ignore


As discussed in above definition, you can use @Ignore annotation to ignore a test
or group of the test.

Let’s understand it using simple example and in below given scenarios:

1. Creating a simple test class without ignoring a test.


2. Ignore a test method using @Ignore annotation.
3. Ignore a test method using @Ignore annotation with proper reason.
4. Ignore all test method using @Ignore annotation.

Creating a simple test class without ignoring a test


Let’s create a simple Java class which prints two types of message.

 First method prints a simple message and


 The second method prints a “hi” message

JUnitMessage.java
package guru99.junit;

public class JUnitMessage {

private String message;

public JUnitMessage(String message) {


this.message = message;
}

public String printMessage(){


System.out.println(message);

return message;
}

public String printHiMessage(){

message="Hi!"+ message;

System.out.println(message);

return message;
}

}
JunitTestExample.java

Let’s create a JUnit test class to test JUnitMessage.java.

In this JUnit test class,

 First test, named “testJUnitMessage()” tests “printMessage()” method of


above class.
 Similarly the second test, named “testJUnitHiMessage” tests
“testJUnitHiMessage” of above class.

package guru99.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class JunitTestExample {

public String message = "Guru99";

JUnitMessage junitMessage = new JUnitMessage(message);

@Test
public void testJUnitMessage() {

System.out.println("Junit Message is printing");


assertEquals(message, junitMessage.printMessage());

@Test
public void testJUnitHiMessage() {
message="Hi!" +message;
System.out.println("Junit Hi Message is printing ");
assertEquals(message, junitMessage.printHiMessage());

}
}
TestRunner.java
Let’s create a test runner class to execute JunitTestExample.java

package guru99.junit;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(JunitTestExample.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
Output:

Print statement on console:

Junit Hi Message is printing

Hi!Guru99

Junit Message is printing

Guru99

Ignore a test method using @Ignore annotation


Let’s create ignore test to disable a test in above example. For this, you need to use
@Ignore in the method, you want to skip.

Let’s do it for testJUnitMessage() of JunitTestExample.java

JunitTestExample.java

package guru99.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;
public class JunitTestExample {

public String message = "Guru99";

JUnitMessage junitMessage = new JUnitMessage(message);

@Ignore
@Test
public void testJUnitMessage() {

System.out.println("Junit Message is printing ");


assertEquals(message, junitMessage.printMessage());

@Test
public void testJUnitHiMessage() {
message="Hi!" +message;
System.out.println("Junit Hi Message is printing ");
assertEquals(message, junitMessage.printHiMessage());

}
}
Output:

Let’s execute and verify the output of above example.

Below output shows that one test is skipped (disabled), see as marked below:

Print statement on console:

Junit Hi Message is printing

Hi!Guru99

Using @ ignore annotation with Condition


Let’s take the example of how to ignore a test and define the reason for ignoring
along with it. As discussed above, to provide a reason you have one optional
parameter in @Ignore annotation where you can provide the reason statement.

JunitTestExample.java
package guru99.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;

public class JunitTestExample {

public String message = "Guru99";

JUnitMessage junitMessage = new JUnitMessage(message);

@Ignore("not yet ready , Please ignore.")


@Test
public void testJUnitMessage() {

System.out.println("Junit Message is printing ");


assertEquals(message, junitMessage.printMessage());

@Test
public void testJUnitHiMessage() {
message="Hi!" +message;
System.out.println("Junit Hi Message is printing ");
assertEquals(message, junitMessage.printHiMessage());

}
}
Output:

Same as above.

Ignore all test methods using @Ignore annotation.


As discussed above to ignore all the tests in class, you need to use @Ignore
annotation at the class level.

Let’s modify above example to understand how to ignore all the tests:

package guru99.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Ignore;
import org.junit.Test;

@Ignore
public class JunitTestExample {

public String message = "Guru99";

JUnitMessage junitMessage = new JUnitMessage(message);

@Test
public void testJUnitMessage() {
System.out.println("Junit Message is printing ");
assertEquals(message, junitMessage.printMessage());

@Test
public void testJUnitHiMessage() {
message="Hi!" +message;
System.out.println("Junit Hi Message is printing ");
assertEquals(message, junitMessage.printHiMessage());

}
}
Output :

Print statement on console:

As both the tests skipped by using @Ignore at class level so no statement would be
printed on the console.

Summary:
In this tutorial, you learned how to ignore a single test, group of test or all tests by
using @Ignore annotation.

@Ignore annotation can be used in two scenarios as given below:

1. If you want to ignore a test method, use @Ignore along with @Test
annotation.
2. If you want to ignore all the tests of class, use @Ignore annotation at the
class level.

You also learned how to provide a statement to make understand other developer,
why a particular test is disabled.

Lesson 7: JUnit Expected Exception Test:


@Test(expected)
JUnit provides the facility to trace the exception and also to check whether the
code is throwing expected exception or not.
Junit4 provides an easy and readable way for exception testing, you can use

 Optional parameter (expected) of @test annotation and


 To trace the information ,”fail()” can be used

While Testing exception, you need to ensure that exception class you are providing
in that optional parameter of @test annotation is the same. This is because you
are expecting an exception from the method you are Unit Testing, otherwise our
JUnit test would fail.

Example@Test(expected=IllegalArgumentException.class)

By using “expected” parameter, you can specify the exception name our test may
throw. In above example, you are using “IllegalArgumentException” which will be
thrown by the test if a developer uses an argument which is not permitted.

Example using @test(expected)


Let’s understand exception testing by creating a Java class with a method
throwing an exception. You will handle it and test it in a test class. Consider
JUnitMessage.java having a method which simply do a mathematical operation
based on input received by the user. If any illegal argument would be entered, it
will throw “ArithmeticException“. See below:
package guru99.junit;

public class JUnitMessage{

private String message;

public JUnitMessage(String message) {


this.message = message;
}

public void printMessage(){

System.out.println(message);
int divide=1/0;

public String printHiMessage(){

message="Hi!" + message;

System.out.println(message);

return message;
}

}
Code Explanation:
 Code Line 7: Creating a parameterized constructor with field initialization.
 Code Line 11-14: Creating a method for mathematical operation.
 Code Line 18: Creating another method to print a message.
 Code Line 20: Creating a new string to print a message.
 Code Line 21: Printing new message created in line 20.

Let’s create a test class for above java class to verify exception.

See below test class to unit test exception (ArithmeticException here) throwing
from above java class:

AirthematicTest.java

package guru99.junit;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class AirthematicTest {

public String message = "Saurabh";

JUnitMessage junitMessage = new JUnitMessage(message);

@Test(expected = ArithmeticException.class)
public void testJUnitMessage(){
System.out.println("Junit Message is printing ");
junitMessage.printMessage();

@Test
public void testJUnitHiMessage(){
message="Hi!" + message;
System.out.println("Junit Message is printing ");
assertEquals(message, junitMessage.printMessage());

}
}
Code Explanation:

 Code Line 13: Using @Test annotation to create our test. As you execute
above classes method, it will invoke a mathematical operation. Here
Arithmetic Exception is expected, so you are listing it out as a parameter in
@Test.
 Code Line 17: Invoking printMessage() JUnitMessage.java
 Code Line 18: Creating another method to print HI message.

If you execute this test class, the test method is executed with each defined
parameter. In the above example, the test method is executed five times.

Let’s execute it and verify the result. See below the test runner class to
execute JunitTestExample.java

Output:

Here is the output which shows successful test with no failure trace as given below:

Summary:
 Exception testing is a special feature introduced in JUnit4. In this tutorial,
you have learned how to test exception in JUnit using @test(excepted)
 Junit provides the facility to trace the exception and also to check whether
the code is throwing exception or not
 For exception testing, you can use
 Optional parameter (expected) of @test annotation and
 To trace the information ,”fail()” can be used

Lesson 8: JUnit ErrorCollector @Rule with


Example
In a normal scenario, whenever you identify any error during test execution, you
would stop the test, fix the error and re-run the test.

But JUnit has a slightly different approach. With JUnit error collector, you can still
continue with the test execution even after an issue is found or test fails. Error
collector collects all error objects and reports it only once after the test execution is
over.

Why use Error Collector?


While writing a test script, you want to execute all the tests even if any line of code
fails due to network failure, assertion failure, or any other reason. In that situation,
you can still continue executing test script using a special feature provided by
JUnit known as “error collector.”

For this, JUnit uses @Rule annotation which is used to create an object of error


collector. Once the object for error collector is created, you can easily add all the
errors into the object using method addError (Throwable error). As you know,
that Throwable is the super class of Exception and Error class in Java. When you
add errors in this way, these errors will be logged in JUnit test result .

The benefit of adding all errors in an Error Collector is that you can verify all the
errors at once. Also, if the script fails in the middle, it can still continue executing it

Note: In the case of using simple assert or try/catch block , using error collector
method won’t be possible.

Sample code

To understand more on Error Collector, see below code example which


demonstrates how to create an Error Collector object and add all the errors in that
object to track the issue :

package guru99.junit;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class ErrorCollectorExample {


@Rule
public ErrorCollector collector = new ErrorCollector();

@Test
public void example() {
collector.addError(new Throwable("There is an error in first line"));
collector.addError(new Throwable("There is an error in second line"));
collector.checkThat(getResults(),
not(containsString("here is an error")));
// all lines of code will execute and at the end a combined failure will
be logged in.
}
}

What is @Rule in jUnit?


JUnit provides special kind of handling of tests, Test Case or test suite by
using @rule annotation. Using @rule, you can easily add or redefine the
behaviour of the test.

There are several built-in rules provided by JUnit API that a tester can use, or even
you can write our own rule.

See below line of code, which shows how to use @rule annotation along with Error
Collector:

@Rule
public ErrorCollector collector= new ErrorCollector();

Example using ErrorCollector


To understand error collector, let’s create a class and a rule to collect all the errors.
You will add all the errors using addError(throwable) here.

See below code which simply creates a rule which is nothing but creating “Error
Collector object.” Which is further used to add all the errors in order to report the
issue at the end:

ErrorCollectorExample.java

package guru99.junit;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ErrorCollector;

public class ErrorCollectorExample {


@Rule
public ErrorCollector collector = new ErrorCollector();

@Test
public void example() {
collector.addError(new Throwable("There is an error in first line"));
collector.addError(new Throwable("There is an error in second line"));

System.out.println("Hello");
try {
Assert.assertTrue("A " == "B");
} catch (Throwable t) {
collector.addError(t);
}
System.out.println("World!!!!");
}
}
TestRunner.java

Let’s add above test class in a test runner and execute it to collect all errors. See
below code:

package guru99.junit;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {


public static void main(String[] args) {
Result result = JUnitCore.runClasses(ErrorCollectorExample.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Result=="+result.wasSuccessful());
}
}
Output:

See the failure trace which traces all the errors in one place:
Benefits of JUnit ErrorCollector
You can use JUnit assertion for functional or GUI validation e.g.

1. assertEquals(String message, Object expected, Object actual) which


compare that two objects are equals.
2. Similarly, assertTrue(Boolean condition) asserts that a condition is true.

Using assertion, validation test becomes easy. But one major issue is that test
execution will stop even if a single assertion fails.

Test continuity and recovery handling is crucial to test automation success. Error
Collector is the best way to handle such kind of scenarios.
Summary:
 Junit error collector allows a test to continue even after the first issue is
found and test fails at the end
 Error collector collects all error objects and reports it only, after all, the test
execution over
 The benefit of adding all errors in an Error Collector is that you can verify all
the errors at once
 Error collector simply adds errors using method addError(throwable err)
provided by ErrorCollector.java.

Lesson 9: JUnit Parameterized Test with


Example using @Parameters
What is Parameterized Test in Junit?
Parameterized test is to execute the same test over and over again using different
values. It helps developer to save time in executing same test which differs only in
their inputs and expected results.

Using Parameterized test, one can set up a test method that retrieves data from
some data source.

Consider a simple test to sum different numbers. The code may look like –

The approach above leads to lot of redundancy.

We need a simple approach and. Using parameterized test you can just add a
method to input 10 data inputs and your test will run 10 times automatically.

Steps to create a Parameterized JUnit test


Following code shows an example for a parameterized test. It tests sum() method
of the Arithmetic class :
Step 1) Create a class. In this example, we are going to input two numbers by using
sum (int,int) method which will return the sum of given numbers

Step 2) Create a parameterized test class

Code Explanation

 Code Line 11: Annotate your test class using


@runWith(Parameterized.class).
 Code Line 13: Declaring the variable ‘firstNumber’ as private and type as int.
 Code Line 14: Declaring the variable ‘secondNumber’as private and type as
int.
 Code Line 15: Declaring the variable ‘expectedResult’as private and type as
int.
 Code Line 16: Declaring the variable ‘airthematic’as private and type as
Airthematic.

@RunWith(class_name.class): @RunWith annotation is used to specify its runner


class name. If we don’t specify any type as a parameter, the runtime will
choose BlockJunit4ClassRunner by default.

This class is responsible for tests to run with a new test instance. It is responsible
for invoking JUnit lifecycle methods such as setup(associate resources) and
teardown(release resources).

To parameterize you need to annotate using @RunWith and pass required .class to
be tested

Step 3) Create a constructor that stores the test data. It stores 3 variables
Step 4) Create a static method that generates and returns test data.

Code Line 32,33: Creating a two-dimensional array (providing input parameters


for addition). Using asList method we convert the data into a List type. Since, the
return type of method input is collection.

Code Line 30: Using @Parameters annotation to create a set of input data to run


our test.

The static method identified by @Parameters annotation returns a Collection


where each entry in the Collection will be the input data for one iteration of the
test.

Consider the elemenent

{1,2,3}

Here

firstNumber =1

secondNumber=2

expectedResult=3

Here each array element will be passed to the constructor, one at a time as the
class is instantiated multiple times.

Step 5) The complete code


Code Explanation:

 Code Line 25: Using @Before annotation to setup the resources


(Airthematic.class here). The @Before annotation is used here to run before
each test case. It contains precondition of the test.
 Code Line 36: Using @Test annotation to create our test.
 Code Line 39: Creating an assert statement to check whether our sum is
equivalent to what we expected.

Step 6) Create a test runner class to run parameterized test:


Code Explanation:

 Code Line 8: Declaring the main method of the class test which will run our
JUnit test.
 Code Line 9: Executing test cases using JunitCore.runclasses, it will take the
test class name as a parameter (In our example we are using
Airthematic.class).
 Code Line 11: Processing the result using for loop and printing out failed
result.
 Code Line 13: Printing out the successful result.

Output:

Here is the output which shows successful test with no failure trace as given below:
See the result on console,which shows addition of two numbers :-

Summary:

Parameterized test enables developer to execute the same test over and over again
using different values.

Important annotations to be used during parameterization

 @RunWith
 @Parameters
Lesson 10: TestNG Vs JUnit: What’s the
Difference?
Both Testng and Junit are Testing framework used for Unit Testing. TestNG is
similar to JUnit. Few more functionalities are added to it that makes TestNG more
powerful than JUnit.

This tutorial is mainly focused to analyse features of JUnit and TestNG. It help
developers to decide which framework should be used for Unit Testing. Let’s
analyse similarities between TestNG and JUnit4 first.

TestNG is a testing framework inspired by JUnit and NUnit.

Here is the table that shows the features supported by JUnit and TestNG.

JUnit4 and TestNG Feature comparison


Both TestNG and JUnit4 looks similar, except one or two features. Let’s have a
comparison between the two to quickly decide, which technology is more
favourable for Unit Testing. Below table highlights the features supported by both:

Annotations
Both JUnit and TestNG uses annotations and almost all the annotations looks
similar.

TestNG uses @BeforeMethod ,@AfterMethod similar to @Before ,@After in JUnit4.

Both TestNG and Junit4 uses @Test(timeout = 1000) for timeout .Check the table
below for more details-
S.N. Description TestNG JUnit 4

1 Test annotation @Test @Test

Executes before the first


2 test method is invoked in @BeforeClass @BeforeClass
the current class

Executes after all the test


3 methods in the current @AfterClass @AfterClass
class

Executes before each test


4 @BeforeMethod @Before
method

Executes after each test


5 @AfterMethod @After
method

6 annotation to ignore a test @Test(enable=false) @ignore

@Test(expectedExceptions = @Test(expected =
7 annotation for exception
ArithmeticException.class) ArithmeticException.class)

8 timeout @Test(timeout = 1000) @Test(timeout = 1000)

Executes before all tests in


9 @BeforeSuite n/a
the suite

Executes after all tests in


10 @AfterSuite n/a
the suite

11 Executes before a test runs @BeforeTest n/a

12 Executes after a test runs @AfterTest n/a

Executes before the first


test method is invoked that
13 @BeforeGroups n/a
belongs to any of these
groups is invoked

run after the last test


14 method that belongs to @AfterGroups n/a
any of the groups here

Suite Test
Suites are used to execute multiple tests together. Suites can be created using both
TestNG and JUnit4. However, suites are more powerful in TestNG as it uses very
different method for execution of tests. Let’s understand it using code snippet as
given below:

Using JUnit4

Below class describes use of suite while working with JUnit4:

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

@RunWith(Suite.class)
@Suite.SuiteClasses({
SuiteTest1.class,
SuiteTest2.class,

})

public class JunitTest {


// This class remains empty,it is used only as a holder for the above annotations
}
Using TestNG

TestNG uses xml to bundle all tests at one place.Below xml describes use of suite
while working with TestNG:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >


<suite name="My test suite">
<test name="testing">
<classes>
<class name="com.guru99.SuiteTest1" />
<class name="com.guru99.SuiteTest2" />
</classes>
</test>
</suite>

Ignore Test
Using both we can skip a test .Let’s see it using code example as given below:

Using JUnit4

Below code snippet describes use of @ignore annotation while working with
JUnit4:

@Ignore
public void method1()
{
System.out.println("Using @Ignore , this execution is ignored");
}
Using TestNG

Below code snippet describes use of @Test(enabled=false) annotation while


working with TestNG:

@Test(enabled=false)
public void TestWithException()
{
System.out.println("Method should be ignored as it's not ready yet");
}

Exception Test
Exception testing is available both in TestNG and JUnit4. It is used to check, which
exception is thrown from the test?

Using JUnit4

Below code snippet describes use of exception test while working with JUnit4:

@Test(expected = ArithmeticException.class)
public void divideByZero()
{
Int i = 1/0;
}
Using TestNG

Below code snippet describes use of exception test while working with TestNG:

@Test(expectedExceptions = ArithmeticException.class)
public void divideByZero()
{
Int i = 1/0;
}

Timeout
This feature is implemented both in TestNg and JUnit4.Timeout is used to
terminate a test which takes longer than specified time (in milliseconds).

Using JUnit4

Below code snippet describes use of timeout test while working with JUnit4:

@Test(timeout = 1000)
public void method1()
{
while (true);
}
Using TestNG
Below code snippet describes use of timeout test while working with TestNG:

@Test(timeOut = 1000)
public void method1()
{
while (true);
}

Parameterized Test
JUnit provides an easier and readable approach to test known as Parameterized
test. Both TestNG and JUnit supports parameterized test but differ in the way they
define the parameter value. Let see this one by one.

Using JUnit4

The “@RunWith” and “@Parameter” annotations are used to provide parameter


value for the unit test. The annotation @Parameters have to return List[] .This
parameter will be passed into the class constructor as an argument.

@RunWith(value = Parameterized.class)
public class JunitTest{

privateint number;

public JunitTest6(int number)


{
this.number = number;
}

@Parameters
public static Collection<Object[]> data()
{
Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
returnArrays.asList(data);
}

@Test
public void parameterTest()
{
System.out.println("Parameterized Number is : " + number);
}
}
Using TestNG

In TestNG, XML file or “@DataProvider” is used to provide a parameter for testing.

Here @Parameters annotation declared in the method, needs a parameter for


testing. The data used as the parameter will provide in TestNG’s XML configuration
files. By doing this, we can reuse a single Test Case with different data sets, and we
can get different results.

public class Test1 {


@Test
@Parameters(value="number")
public void parameterTest(int number)
{
System.out.println("Parameterized Number is : " + number);
}

}
See below xml file to be used for above class:

<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >


<suite name="My test suite">
<test name="testing">

<parameter name="number" value="2"/>

<classes>
<class name="com.guru99.Test1" />
</classes>
</test>
</suite>

Summary :
We saw JUnit4 and TestNG comparison in details. We also saw both are similar
except parameterized test and dependency test. In short we can say, based on
flexibility and requirement we can choose any one of them for Unit Testing.

You might also like