Testing in Software Engineering:: Question-19
Testing in Software Engineering:: Question-19
Question-19
Test a piece of code which executes a specific functionality in the code to be tested and asserts a certain
behavior or state using the JUnit.
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.
Here, are the key reasons to perform unit testing in software engineering:
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 reuse. Migrate both your code and your tests to your new project. Tweak the
code until the tests run again.
In order to do Unit Testing, developers write a section of code to test a specific function in software application.
Developers generally use Unit Test framework to develop automated test cases for unit testing.
Introduction to JUnit
JUnit is an open source Unit Testing Framework for JAVA. It is useful for Java Developers to write and run
repeatable tests. As the name implies, it is used for Unit Testing of a small chunk of code.
JUnit does not require a server for testing web applications, which makes the testing process fast. JUnit
framework also allows quick and easy generation of test cases and test data.
The org.Junit package consists of many interfaces and classes for JUnit Testing such as Test, Assert, After,
Before, etc.
In the TestJunitTestCaseExample.java, we write the test cases for the JunitTestCaseEample.java class. We
create an object of the JunitTestCaseExample.java class, and by using its object, we will test all its methods.
We create the TestRunner.java class to execute the test cases. It contains the main() method in which we run the
TestJunitTestCaseExample.java class using the runClasses() method
Program :
if (str.length() == 10)
return true;
else
return false;
}
}
Test cases :
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;