0% found this document useful (0 votes)
71 views3 pages

Testing in Software Engineering:: Question-19

Unit testing involves writing code to test individual units or components of an application to validate that each unit performs as expected. JUnit is a popular unit testing framework for Java that allows developers to write and run automated tests. This document provides an example of using JUnit to test a StringHelper class that truncates admission numbers and checks phone numbers, with tests asserting expected outputs.

Uploaded by

Imtiyaz Basha
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)
71 views3 pages

Testing in Software Engineering:: Question-19

Unit testing involves writing code to test individual units or components of an application to validate that each unit performs as expected. JUnit is a popular unit testing framework for Java that allows developers to write and run automated tests. This document provides an example of using JUnit to test a StringHelper class that truncates admission numbers and checks phone numbers, with tests asserting expected outputs.

Uploaded by

Imtiyaz Basha
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/ 3

Software Engineering Lab

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.

Testing in Software Engineering:

Software testing activities can be categorized the following ways-


• Functional activities that ensure if a product meets technical requirements, all the features work as
intended;
• Non-functional activities that validate performance, ease of use, security, and user satisfaction with the
project.

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.

Steps to perform unit testing

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.

Unit Testing is of two types


• Manual
• Automated

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 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.
Workflow of Unit Testing

1) Create Test Cases


2) Review/Rework
3) Baseline
4) Execute Test Cases

Unit Testing Techniques


Black Box Testing - Using which the user interface, input and output are tested.
White Box Testing - used to test each one of those functions behaviour is tested.
Gray Box Testing - Used to execute tests, risks and assessment methods.

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.

Testing Code using Junit

In the JunitTestCaseExample.java class, we created the code which we want to test.

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 :

public class StringHelper {

public String truncateAdmNum(String str) {


if (str.length() <= 10)
return str.replaceAll("19001A0", "");

String first7Chars = str.substring(0, 7);


String stringMinusFirst7Chars = str.substring(7);

return first7Chars.replaceAll("19001A0", "") + stringMinusFirst7Chars;


}

public boolean PhoneNum(String str) {

if (str.length() == 10)
return true;
else
return false;
}
}

Test cases :
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

public class StringHelperTest {


StringHelper helper;
@Before
public void before(){
helper = new StringHelper();
}
@Test
public void testTruncateAdmNum() {
assertEquals("599", helper.truncateAdmNum("19001A0599"));
}
@Test
public void testPhoneNum() {
assertTrue(helper.PhoneNum("9000202069"));
}
@Test
public void testPhNum() {
assertFalse(helper.PhoneNum("9000202069"));
}
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy