SlideShare a Scribd company logo
Junit and Cactus

Dr. Himanshu Hora
SRMS College of Engineering & Technology
Bareilly (INDIA)
Table of Contents
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—

Overview
Article Outline
Thesis
JUnit Background
Cactus Background
Pitfalls 1-6
Conclusion
Overview
ď‚— Junit and Cactus are popular tools for automating
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—

testing of Java classes and web-based components
Automation accomplished using ANT (Java Make utility)
and often tested every time project built
These tools enable testers to “verify” the code is written
correctly – it was built right
Automated tests are very useful to show code works
correctly esp. after refactoring which is central to XP
I picked this article because deals with JUnit & Cactus,
offers a critique of the tools, and was practical, not just
theoretical
As this article describes, the tests themselves must be
built right in order to validate the code being tested
Outline
ď‚— Theme: Risk when testing with JUnit & Cactus
ď‚— Risk 1: No assert
ď‚— Risk 2: Unreasonable assert
ď‚— Risk 3: Console-Based Testing
ď‚— Risk 4: Unfocused Test Method
ď‚— Risk 5: Failure To Isolate Each Test
ď‚— Risk 6: Failure to Isolate Subject
Analysis
ď‚— Thesis: critique was correct but wordy, repetive, and
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—

incomplete – missing some bigger pitfalls
Risk 1-3 are all the same – use assert correctly
Risk 4 is programming rule – write focused method
Risk 5 is JUnit rule – Use setUp and tearDown
Risk 6 is incomplete analysis of Cactus vs MockObj
Overview of Junit and Cactus, then discuss pitfalls, and
finally look at unmentioned pitfalls & issues
JUnit Overview
ď‚— Popular and simple Java framework / library for
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—

ď‚—
ď‚—

automating testing
Integrates well with ANT – Java Make utility
General idea: write one test class per testee
Write one method to verify each main feature
Test class must extend TestCase and each test method
must start with “test”
Order of test method execution varies
Use assertTrue() and assertEquals() to verify code
Use setUp() & tearDown() prepare testcase testfixture
JUnit code example
Following test cases test the collection methods, isEmpty() and add()
import junit.framework.*;
public class SimpleTest extends TestCase {
private java.uti.Collection collection;
protected void setUp() { collection = new ArrayList(); } //
instantiates collection test fixture
protected void tearDown() { collection.clear(); }
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}
Cactus Overview
ď‚— Built on Junit framework
ď‚— Intended to test JSP, Servlets, EJBs, Filters, and custom

tags
ď‚— Complex architecture that has client JVM call the J2EE
application server JVM via redirector
ď‚— Testcase classes must reside on client and server
ď‚— Adds two methods to Junit architecture, beginXX() and
endXX() which get called on client, rest on server
Cactus System Diagram
Cactus Sequence Diagram

– Jakarta website
Risk 1-3 – No assert, Unreasonable assert,
Console-Based Testing
 Risk 1-3 are all the same – use assert correctly
ď‚— Very important and author points out a major guideline
ď‚— test what is written in the javadocs for the testee
ď‚— implies javadocs must be up-to-date with requirements

ď‚— Author also points out to write a test case if encounter a

defect before it is corrected
ď‚— However, using assertTrue() and assertEquals() is
obvious
ď‚— These are the prominent features of the JUnit
Risk 4 – Unfocused Test Methods
ď‚— Writing focused tests is really just writing good code
ď‚— General rule of programming to make methods succinct,

this applies equally to test methods
ď‚— Writing focused test methods is the whole point
Risk 5 – Failure To Isolate Each
Test

Risk 5 is really saying to use setUp() and tearDown() to prepare/release test
fixture, an obvious suggestion – example from JUnit site
Bigger pitfall is automating creation of the test fixture in distributed
environments
import junit.framework.*;
public class SimpleTest extends TestCase {
private java.uti.Collection collection;
protected void setUp() { collection = new ArrayList(); } // instantiates
collection test fixture for 2 tests
protected void tearDown() { collection.clear(); }
public void testEmptyCollection() {
assertTrue(collection.isEmpty());
}
public void testOneItemCollection() {
collection.add("itemA");
assertEquals(1, collection.size());
}
}
Risk 6 – Failure to Isolate Subject
ď‚— Author points out one drawback of Cactus is that does

not isolate test case as MockObjects does
ď‚— MockObjects simulates the Servlet container
ď‚— Mock Object framework does isolate test but at big expense

ď‚— Massive amount of stubs needed, more code to maintain

ď‚— While Cactus may be better than MockObjects, it may

NOT be better than HttpUnit, why not compare these?
Alternative tools
ď‚— HttpUnit cleaner, simpler tool than Cactus
ď‚— HttpUnit is black box testing by calling webserver
ď‚— Test code resides ONLY on client JVM

ď‚— Various interfaces like JWebUnit (Java API) and
ď‚—
ď‚—
ď‚—
ď‚—

WebTest (XML) integrate well with ANT
Use Junit for unit tests and HttpUnit for functional
Features to analyze HTML, ie. table element tests
Features to input HTML form elements
http://www.junit.org/news/extension/index.htm
Conclusion
ď‚— Article lists some useful guidelines & pitfalls in an wordy fashion
ď‚— Many pitfalls were obvious and important ones not mentioned
ď‚— Important pitfalls not mentioned include
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—
ď‚—

Cost, complexity, difficulty of distributed tests not mentioned
Performs white box tests, yet, JUnit already does this
Does not test HTTP interface (tests presentation layer poorly)
Test code must reside in same package as testee & both JVMs
Testers must be programmers
JWebUnit & WebTest better for web unit testing

ď‚— At times unclear when addressing Junit vs. Cactus and unnecessarily
complex coding examples
ď‚— However, automating testing can save time and money in the long run
ď‚— These tools, while not perfect, are major players for automated Java
testing and can verify functionality during development and refactoring
THANK YOU
Dr. Himanshu Hora
SRMS College of Engineering & Technology
Bareilly (INDIA)

More Related Content

PPTX
Unix Operating System
PPTX
Operating System Structure
PPTX
Operating systems
PPTX
UNIX Operating System
PDF
Operating systems Basics
PPTX
11. operating-systems-part-2
PDF
Forensics of a Windows System
PPTX
Big Data to avoid weather related flight delays
Unix Operating System
Operating System Structure
Operating systems
UNIX Operating System
Operating systems Basics
11. operating-systems-part-2
Forensics of a Windows System
Big Data to avoid weather related flight delays

What's hot (20)

PPTX
Network and System Administration chapter 2
PPTX
08. networking
PDF
CS6004 Cyber Forensics
PPTX
Voicexml ppt
PPT
08 Operating System Support
PPT
Linux forensics
PDF
BIG DATA TO AVOID WEATHER RELATED FLIGHT DELAYS PPT
PPT
User administration concepts and mechanisms
PPTX
Intrusion detection systems
PPTX
RTOS- Real Time Operating Systems
DOCX
operating system question bank
PPTX
Operating system 11 system calls
PDF
PPTX
PARALLELISM IN MULTICORE PROCESSORS
PPT
Introduction to ms dos
PDF
Concurrent/ parallel programming
PPT
Multiprocessor Systems
PDF
operating system structure
PPTX
Unix ppt
PPTX
System calls
Network and System Administration chapter 2
08. networking
CS6004 Cyber Forensics
Voicexml ppt
08 Operating System Support
Linux forensics
BIG DATA TO AVOID WEATHER RELATED FLIGHT DELAYS PPT
User administration concepts and mechanisms
Intrusion detection systems
RTOS- Real Time Operating Systems
operating system question bank
Operating system 11 system calls
PARALLELISM IN MULTICORE PROCESSORS
Introduction to ms dos
Concurrent/ parallel programming
Multiprocessor Systems
operating system structure
Unix ppt
System calls
Ad

Similar to Junit and cactus (20)

DOCX
Test Driven Development
PPT
Testing Options in Java
PPTX
Unit Testing in Java
DOCX
JUnit_Guide_Expanded_Presentation[1].docx
DOCX
JUnit_Guide_Expanded_Presentation[1].docx............................
DOCX
JUnit_Guide_Expanded_Presentation[1].docx
PPT
Unit testing php-unit - phing - selenium_v2
PPTX
8-testing.pptx
PPS
JUnit Presentation
PPS
J unit presentation
PPTX
Junit_.pptx
PPT
Google test training
PPTX
Junit4&testng presentation
PPTX
SE2018_Lec 20_ Test-Driven Development (TDD)
PPTX
Testes? Mas isso nĂŁo aumenta o tempo de projecto? NĂŁo quero...
PDF
SE2_Lec 21_ TDD and Junit
PPT
Assessing Unit Test Quality
PPTX
JUnit- A Unit Testing Framework
PDF
New and improved: Coming changes to the unittest module
PPTX
Introduction to JUnit testing in OpenDaylight
Test Driven Development
Testing Options in Java
Unit Testing in Java
JUnit_Guide_Expanded_Presentation[1].docx
JUnit_Guide_Expanded_Presentation[1].docx............................
JUnit_Guide_Expanded_Presentation[1].docx
Unit testing php-unit - phing - selenium_v2
8-testing.pptx
JUnit Presentation
J unit presentation
Junit_.pptx
Google test training
Junit4&testng presentation
SE2018_Lec 20_ Test-Driven Development (TDD)
Testes? Mas isso nĂŁo aumenta o tempo de projecto? NĂŁo quero...
SE2_Lec 21_ TDD and Junit
Assessing Unit Test Quality
JUnit- A Unit Testing Framework
New and improved: Coming changes to the unittest module
Introduction to JUnit testing in OpenDaylight
Ad

More from Himanshu (20)

PPT
Structural patterns
PPTX
Software product line
PPT
Shared information systems
PPTX
Saam
PPTX
Design Pattern
PPTX
Creational pattern
PPTX
Architecture Review
PPTX
Reliability and its principals
PPTX
Structural and functional testing
PPTX
White box black box & gray box testing
PPTX
Pareto analysis
PPTX
Load runner & win runner
PPTX
Crud and jad
PPTX
Risk based testing and random testing
PPTX
Testing a data warehouses
PPTX
Software testing tools and its taxonomy
PPTX
Software reliability engineering process
PPTX
Software reliability growth model
PPTX
Software reliability tools and common software errors
PPTX
Regression and performance testing
Structural patterns
Software product line
Shared information systems
Saam
Design Pattern
Creational pattern
Architecture Review
Reliability and its principals
Structural and functional testing
White box black box & gray box testing
Pareto analysis
Load runner & win runner
Crud and jad
Risk based testing and random testing
Testing a data warehouses
Software testing tools and its taxonomy
Software reliability engineering process
Software reliability growth model
Software reliability tools and common software errors
Regression and performance testing

Recently uploaded (20)

DOCX
The AUB Centre for AI in Media Proposal.docx
 
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Cloud computing and distributed systems.
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Machine learning based COVID-19 study performance prediction
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
KodekX | Application Modernization Development
 
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
The AUB Centre for AI in Media Proposal.docx
 
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
MYSQL Presentation for SQL database connectivity
Cloud computing and distributed systems.
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
Machine learning based COVID-19 study performance prediction
MIND Revenue Release Quarter 2 2025 Press Release
Diabetes mellitus diagnosis method based random forest with bat algorithm
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
KodekX | Application Modernization Development
 
Programs and apps: productivity, graphics, security and other tools
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx

Junit and cactus

  • 1. Junit and Cactus Dr. Himanshu Hora SRMS College of Engineering & Technology Bareilly (INDIA)
  • 2. Table of Contents ď‚— ď‚— ď‚— ď‚— ď‚— ď‚— ď‚— Overview Article Outline Thesis JUnit Background Cactus Background Pitfalls 1-6 Conclusion
  • 3. Overview ď‚— Junit and Cactus are popular tools for automating ď‚— ď‚— ď‚— ď‚— ď‚— testing of Java classes and web-based components Automation accomplished using ANT (Java Make utility) and often tested every time project built These tools enable testers to “verify” the code is written correctly – it was built right Automated tests are very useful to show code works correctly esp. after refactoring which is central to XP I picked this article because deals with JUnit & Cactus, offers a critique of the tools, and was practical, not just theoretical As this article describes, the tests themselves must be built right in order to validate the code being tested
  • 4. Outline ď‚— Theme: Risk when testing with JUnit & Cactus ď‚— Risk 1: No assert ď‚— Risk 2: Unreasonable assert ď‚— Risk 3: Console-Based Testing ď‚— Risk 4: Unfocused Test Method ď‚— Risk 5: Failure To Isolate Each Test ď‚— Risk 6: Failure to Isolate Subject
  • 5. Analysis ď‚— Thesis: critique was correct but wordy, repetive, and ď‚— ď‚— ď‚— ď‚— ď‚— incomplete – missing some bigger pitfalls Risk 1-3 are all the same – use assert correctly Risk 4 is programming rule – write focused method Risk 5 is JUnit rule – Use setUp and tearDown Risk 6 is incomplete analysis of Cactus vs MockObj Overview of Junit and Cactus, then discuss pitfalls, and finally look at unmentioned pitfalls & issues
  • 6. JUnit Overview ď‚— Popular and simple Java framework / library for ď‚— ď‚— ď‚— ď‚— ď‚— ď‚— ď‚— automating testing Integrates well with ANT – Java Make utility General idea: write one test class per testee Write one method to verify each main feature Test class must extend TestCase and each test method must start with “test” Order of test method execution varies Use assertTrue() and assertEquals() to verify code Use setUp() & tearDown() prepare testcase testfixture
  • 7. JUnit code example Following test cases test the collection methods, isEmpty() and add() import junit.framework.*; public class SimpleTest extends TestCase { private java.uti.Collection collection; protected void setUp() { collection = new ArrayList(); } // instantiates collection test fixture protected void tearDown() { collection.clear(); } public void testEmptyCollection() { assertTrue(collection.isEmpty()); } public void testOneItemCollection() { collection.add("itemA"); assertEquals(1, collection.size()); } }
  • 8. Cactus Overview ď‚— Built on Junit framework ď‚— Intended to test JSP, Servlets, EJBs, Filters, and custom tags ď‚— Complex architecture that has client JVM call the J2EE application server JVM via redirector ď‚— Testcase classes must reside on client and server ď‚— Adds two methods to Junit architecture, beginXX() and endXX() which get called on client, rest on server
  • 11. Risk 1-3 – No assert, Unreasonable assert, Console-Based Testing ď‚— Risk 1-3 are all the same – use assert correctly ď‚— Very important and author points out a major guideline ď‚— test what is written in the javadocs for the testee ď‚— implies javadocs must be up-to-date with requirements ď‚— Author also points out to write a test case if encounter a defect before it is corrected ď‚— However, using assertTrue() and assertEquals() is obvious ď‚— These are the prominent features of the JUnit
  • 12. Risk 4 – Unfocused Test Methods ď‚— Writing focused tests is really just writing good code ď‚— General rule of programming to make methods succinct, this applies equally to test methods ď‚— Writing focused test methods is the whole point
  • 13. Risk 5 – Failure To Isolate Each Test Risk 5 is really saying to use setUp() and tearDown() to prepare/release test fixture, an obvious suggestion – example from JUnit site Bigger pitfall is automating creation of the test fixture in distributed environments import junit.framework.*; public class SimpleTest extends TestCase { private java.uti.Collection collection; protected void setUp() { collection = new ArrayList(); } // instantiates collection test fixture for 2 tests protected void tearDown() { collection.clear(); } public void testEmptyCollection() { assertTrue(collection.isEmpty()); } public void testOneItemCollection() { collection.add("itemA"); assertEquals(1, collection.size()); } }
  • 14. Risk 6 – Failure to Isolate Subject ď‚— Author points out one drawback of Cactus is that does not isolate test case as MockObjects does ď‚— MockObjects simulates the Servlet container ď‚— Mock Object framework does isolate test but at big expense ď‚— Massive amount of stubs needed, more code to maintain ď‚— While Cactus may be better than MockObjects, it may NOT be better than HttpUnit, why not compare these?
  • 15. Alternative tools ď‚— HttpUnit cleaner, simpler tool than Cactus ď‚— HttpUnit is black box testing by calling webserver ď‚— Test code resides ONLY on client JVM ď‚— Various interfaces like JWebUnit (Java API) and ď‚— ď‚— ď‚— ď‚— WebTest (XML) integrate well with ANT Use Junit for unit tests and HttpUnit for functional Features to analyze HTML, ie. table element tests Features to input HTML form elements http://www.junit.org/news/extension/index.htm
  • 16. Conclusion ď‚— Article lists some useful guidelines & pitfalls in an wordy fashion ď‚— Many pitfalls were obvious and important ones not mentioned ď‚— Important pitfalls not mentioned include ď‚— ď‚— ď‚— ď‚— ď‚— ď‚— Cost, complexity, difficulty of distributed tests not mentioned Performs white box tests, yet, JUnit already does this Does not test HTTP interface (tests presentation layer poorly) Test code must reside in same package as testee & both JVMs Testers must be programmers JWebUnit & WebTest better for web unit testing ď‚— At times unclear when addressing Junit vs. Cactus and unnecessarily complex coding examples ď‚— However, automating testing can save time and money in the long run ď‚— These tools, while not perfect, are major players for automated Java testing and can verify functionality during development and refactoring
  • 17. THANK YOU Dr. Himanshu Hora SRMS College of Engineering & Technology Bareilly (INDIA)