Junit Solutions Web Applications and Services. Standing on the shoulders of giants l Special thanks to: Mike Clark JUnit FAQ Maintainer www.clarkware.com.

Slides:



Advertisements
Similar presentations
Unit Testing Australian Development Centre Brisbane, Australia.
Advertisements

J-Unit Framework.
Apache Struts Technology
An Introduction to JUnit Greg Jackson June Software Quality Assurance & Testing 2 Contact Info Northrop Grumman Corp
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Approach of Unit testing with the help of JUnit Satish Mishra
1 Software Testing and Quality Assurance Lecture 23 – JUnit Tutorial.
JUnit Syed Nabeel. Motivation Unit Testing Responsibility of  developer Rarely done properly Developers Excuse: “I am too much in a hurry”
JUnit, Revisited 17-Apr-17.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
22-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
24-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
26-Jun-15 JUnit. 2 Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (running whatever tests.
Writing a Unit test Using JUnit At the top of the file include: import junit.framework.TestCase; The main class of the file must be: public Must extend.
Testowanie kodu Bartosz Baliś, Na podstawie prezentacji Satisha Mishra Iana Sommerville Erica Braude.
Software Development Tools COMP220 Seb Coope Ant, Testing and JUnit (3) Capturing test results These slides are mainly based on “Java Development with.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
13-Jul-15 Refactoring II. Books Design Patterns is the classic book by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides Basically a catalog.
Apache Struts Technology A MVC Framework for Java Web Applications.
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
Programmer Testing Testing all things Java using JUnit and extensions.
Struts 2.0 an Overview ( )
TDD,BDD and Unit Testing in Ruby
© Dr. A. Williams, Fall Present Software Quality Assurance – JUnit Lab 1 JUnit A unit test framework for Java –Authors: Erich Gamma, Kent Beck Objective:
Lesson 7 Unit Testing /JUnit/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Junit Training Chris Yeung 8 th Sept, Introduction JUnit is a regression testing frameworkJUnit is a regression testing framework Written by Erich.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
Computer Science and Engineering College of Engineering The Ohio State University JUnit The credit for these slides goes to Professor Paul Sivilotti at.
DbUnit Framework Joe Borosky Chris DeCelles Yvonne Krashkevich.
Unit testing Unit testing TDD with JUnit. Unit Testing Unit testing with JUnit 2 Testing concepts Unit testing Testing tools JUnit Practical use of tools.
CSC 216/001 Lecture 4. Unit Testing  Why is it called “unit” testing?  When should tests be written?  Before the code for a class is written.  After.
Junit At the forefront of Test Driven Development.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
JUnit Dwight Deugo Nesa Matic
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
JUnit Adam Heath. What is JUnit?  JUnit is a unit testing framework for the Java programming language  It allows developers to swiftly and easily test.
JUnit Dwight Deugo Nesa Matic
A tool for test-driven development
By Rick Mercer with help from Kent Beck and Scott Ambler Java Review via Test Driven Development (TDD)
JUnit Jumpstart © Manning Publications.
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Don Braffitt Updated: 10-Jun-2011.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Chapter 1 Introducing Ant. What is ant? Ant is a build tool  Automate the tasks of compiling code, running test, and packaging the results for redistribution.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 CSC 216 Lecture 3. 2 Unit Testing  The most basic kind of testing is called unit testing  Why is it called “unit” testing?  When should tests be.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Test a Little, Code a Little Colin Sharples IBM Global Services New Zealand Colin Sharples IBM Global Services New Zealand.
JUnit, Bugzilla James Atlas July 24, 2008 *part of today’s slides courtesy of Dwight Deugo and Nesa Matic under the EPL.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Unit Testing with FlexUnit
1 JUnit. 2 Unit Testing with JUnit If code has no automated test case written for it to prove that it works, it must be assumed not to work. An API that.
Apache Struts Technology A MVC Framework for Java Web Applications.
Test automation / JUnit Building automatically repeatable test suites.
Getting Started with JUnit Getting Started with JUnit The benefits and ease of writing and running JUnit test cases and test suites. The benefits and ease.
SWE 434 SOFTWARE TESTING AND VALIDATION LAB2 – INTRODUCTION TO JUNIT 1 SWE 434 Lab.
Software Development Tools
Software Construction Lab 10 Unit Testing with JUnit
Don Braffitt Updated: 26-Mar-2013
Introduction to JUnit CS 4501 / 6501 Software Testing
Unit testing Java programs Using JUnit
More JUnit CS 4501 / 6501 Software Testing
Test Driven Development 1 November Agenda  What is TDD ?  Steps to start  Refactoring  TDD terminology  Benefits  JUnit  Mocktio  Continuous.
Software Engineering 1, CS 355 Unit Testing with JUnit
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
More JUnit CS 4501 / 6501 Software Testing
JUnit Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from the Eclipse 3.0 and.
Continuous Integration
Presentation transcript:

Junit Solutions Web Applications and Services

Standing on the shoulders of giants l Special thanks to: Mike Clark JUnit FAQ Maintainer Glenn Vanderburg

Philosophy “Any program feature without an automated test simply doesn’t exist.” from Extreme Programming Explained, Kent Beck

Philosophy l Test-driven development l Refactoring l Evolution / emergence

Analogy “It's like trying to build a pyramid by starting at the top, rather than the bottom. No matter how hard you try, you're doomed to failure.” from

Testing pyramid API Developer time Delivered application Angry customers Iteration builds Customer / developer rework

Assertions l Software must and will change over time. Right? l We desire clean uncluttered code. Right? l Can we do both? If so, how?

Refactoring Restructuring of software by applying a series of internal changes that do not affect its observable behavior Fowler, Refactoring, 1999

What is JUnit l De facto Java unit testing framework l Integrated nicely with IDEs and Ant l Easy to learn

Our first unit test package org.example.antbook.common; import junit.framework.TestCase; public class SearchUtilTest extends TestCase { public void testSearch() throws Exception { // right API? Document[] docs = SearchUtil.findDocuments("erik"); assertTrue(docs.length > 0); }

Lean and green package org.example.antbook.common; public class SearchUtil { public static final Document[] findDocuments(String queryString) throws SearchQueryException, SystemException { Document[] results = new Document[1]; return results; }

Test Runners l Text l Swing l Ant Demo

JUnit Assertions l assertTrue(boolean condition) assertFalse(boolean condition) l assertEquals(Object expected, Object actual) Uses equals() comparison Overloaded for all primitive types l assertSame(Object expected, Object actual) assertNotSame(Object expected, Object actual) Uses == comparison l assertEquals(float expected, float actual, float tolerance) l assertNull(Object o) assertNotNull(Object o) l fail(String message) + overloaded String methods

JUnit Design - Pattern dense Assert TestCaseTestSuite > Test YourTest * Courtesy of Mike Clark

JUnit Rules and Conventions Subclass TestCase Prior to v3.8, String-arg constructor required l Test methods public void testXXX() [throws …] Any number of assertions per method Implement main to run from command-line, but not necessary l Optionally add setUp / tearDown methods.

Test fixture package org.example.antbook.ant.lucene; import java.io.IOException; import junit.framework.TestCase; public class HtmlDocumentTest extends DocumentTestCase { HtmlDocument doc; public void setUp() throws IOException { doc = new HtmlDocument(getFile("test.html")); } public void testDoc() { assertEquals("Title", "Test Title", doc.getTitle()); assertEquals("Body", "This is some test", doc.getBodyText()); } public void tearDown() { doc = null; }

TestCase lifecycle setUp testXXX() tearDown() Repeats 1 through 3 for each testXXX method…

Test Suites package org.example.antbook; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.example.antbook.junit.SimpleTest; import org.example.antbook.ant.lucene.HtmlDocumentTest; public class AllTests { static public Test suite() { TestSuite suite = new TestSuite(); suite.addTestSuite(SimpleTest.class); suite.addTestSuite(HtmlDocumentTest.class); return suite; } Demo

JUnit Best Practices l Separate production and test code l But typically in the same packages l Compile into separate trees, allowing deployment without tests l Don’t forget OO techniques, base classing l Test-driven development –Write failing test first –Write enough code to pass –Refactor –Run tests again –Repeat until software meets goal –Write new code only when test is failing

Base classing l Groups of tests share common needs package org.example.antbook.ant.lucene; import java.io.File; import java.io.IOException; import junit.framework.TestCase; public abstract class DocumentTestCase extends TestCase { protected File getFile(String filename) throws IOException { String fullname = this.getClass().getResource(filename).getFile(); File file = new File(fullname); return file; }

Ant and JUnit l Two great tastes that taste great together l You’re building with Ant already, now test with it too! l Facilitates continuous integration and testing l Richer and more flexible reporting than otherwise possible, including publishing and ing l Fail builds if tests fail

task

and formatters <fileset dir="${test.dir}” includes="**/*Test.class” />

Tricks of the trade <junit printsummary="no" errorProperty="test.failed" failureProperty="test.failed" fork="${junit.fork}"> <test name="${testcase}" todir="${test.data.dir}" if="testcase"/>

l Aggregates XML generated by XML formatter output l Transforms result using XSLT into report l Default “frames” and “noframes” HTML stylesheets built-in, but customizable

syntax <report format="frames” todir="${test.reports.dir}" />

Demo

Ant Best Practices l Use naming conventions: *Test.java *TestCase.java Fork, typically Use errorproperty and failureproperty Ironically, don’t failonerror Use after l Use Ant properties for location of XML results and reports resources to build directory Pass parameters using

test data

Passing parameters to tests <junit printsummary="no”… <sysproperty key="docs.dir” file="${test.dir}” /> <sysproperty key="index.dir” file="${test.dir}/index” /> … private String docsDir = System.getProperty("docs.dir"); private String indexDir = System.getProperty("index.dir");

Cactus l In-container unit testing l Excellent for testing: EJB Servlets, Filters, Taglibs Container-dependent frameworks, like Struts

Cactus Architecture

Cactus test case package org.example.antbook; import org.apache.cactus.ServletTestCase; import org.apache.cactus.WebRequest; public class RequestUtilTest extends ServletTestCase { public void beginGetValueParam(WebRequest theRequest) { theRequest.setURL("localhost:8080", "/antbook", "/test/test.jsp", null, "param=url"); } public void testGetValueParam() { request.setAttribute("param", "request"); assertEquals("url", RequestUtil.getValue(request, "param")); } public void testGetValueAttribute() { request.setAttribute("param", "request"); assertEquals("request", RequestUtil.getValue(request, "param")); }

Back to green package org.example.antbook; import javax.servlet.http.HttpServletRequest; public class RequestUtil { public static final String getValue (HttpServletRequest request, String key) { String value = request.getParameter(key); if (value != null) { return value; } value = (String) request.getAttribute(key); if (value != null) { return value; } return null; }

Cactus details l task to start, test, stop l Deploy Tests Cactus APIs Cactus-enabling web.xml

XDoclet diversion - conditional Cactus <webdoclet destdir="${build.dir}/web/WEB-INF" force="${xdoclet.force}" mergedir="metadata/web"> <configParam name="cactusOn” value="${enable.cactus}” /> <deploymentdescriptor validatexml="true" destdir="${build.dir}/${site}" />

XDoclet web.xml merge point <XDtConfig:ifConfigParamEquals paramName="cactusOn” value="true"> ServletRedirector org.apache.cactus.server.ServletTestRedirector ServletTestRunner org.apache.cactus.server.runner.ServletTestRunner

StrutsTestCase l Runs as Mock or on Cactus l Provides assertions for expected Struts ActionErrors and forwards

StrutsTestCase Example package org.example.antbook.struts; import servletunit.struts.CactusStrutsTestCase; public class SearchFormTest extends CactusStrutsTestCase { public SearchFormTest(String s) { super(s); } public void testValidation() { addRequestParameter("query",""); setRequestPathInfo("/search"); actionPerform(); verifyActionErrors(new String[] {"query.required"}); verifyInputForward(); }

Cactus - Servlet Test Runner l New addition, allows tests to be run through browser l XML results returned l Using XSLT capable browser, report transformed to HTML on the fly

Other JUnit Extensions l HttpUnit Parses HTML results into DOM Easy link navigation and form population Useful for automated acceptance tests l Canoo WebTest HttpUnit inside Ant l JUnitPerf Wrap any JUnit tests Measure desired performance and scalability tolerances

xUnit l JUnit l NUnit l CppUnit l RubyUnit l XMLUnit l dbUnit l Etc, etc, etc

Continuous Integration l Build often, triggered by commit even l Anthill Maciej & Urbancode rock! l CruiseControl l Gump

Unit testing issues l How do I test database dependent code? dbUnit l Should I test my user interface? How? HttpUnit Canoo WebTest l But, before you test at these levels, see if refactoring is possible

Code Coverage l Clearly see how much is being tested

Conclusions l “Any program feature without an automated test simply doesn’t exist” l Testable code improves confidence and design l Easy! l “Keep the bar green to keep the code clean!”

Online Resources l JUnit.org l Cactus l Clover l dbUnit l HttpUnit l Canoo WebTest l Mike Clark’s site l Glenn’s reference card

Book Resources l eXtreme Programming Explained: Embrace Change & Test Driven Design Kent Beck l Refactoring: Improving the Design of Existing Code Martin Fowler (Addison-Wesley, 1999) l Java Tools for Extreme Programming Rick Hightower and Nick Lesiecki (Wiley, 2001)

Last but not least… l Chapter 4: Testing with JUnit l Chapter 12: coverage of Cactus l Chapter 15: Testing web services l Test-centric throughout

The End l Some examples provided on the symposium CD l Examples from my book freely available at See Sections/Learning/ch04 specifically l Thank You! l Q & A assertTrue(you.willReturnSpeakerEvaluation())