Testing and Build JUnit and Ant. JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development-

Slides:



Advertisements
Similar presentations
JUnit Tutorial Hong Qing Yu Nov JUnit Tutorial The testing problems The framework of JUnit A case study JUnit tool Practices.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Computer Science 209 Testing With JUnit. Why Test? I don ’ t have time, I ’ ve got a deadline to meet The more pressure I feel, the fewer tests I will.
J-Unit Framework.
ANT – Another Neat Tool Representation and Management of Data on the Internet.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Programming Tools Eclipse JUnit Testing make and ant.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Overview  What is a framework?  JUnit — a simple testing framework.
Presented by IBM developer Works ibm.com/developerworks/ 2006 January – April © 2006 IBM Corporation. Making the most of The Java Development Tools project.
JUnit Introduction and Advanced Features. Topics Covered  Junit Introduction  Fixtures  Test Suites  Currency Example.
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.
3. A Testing Framework. © O. Nierstrasz P2 — A Testing Framework 3.2 A Testing Framework Sources  JUnit 4.0 documentation (from
© S. Demeyer, S. Ducasse, O. Nierstrasz Chapter.1 Unit Testing Explained How to support changes? How to support basic but synchronized documentation?
Guide To UNIX Using Linux Third Edition
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
A Unit Testing Framework: JUnit COMP 302 Software Engineering Koc University, Istanbul.
JUnitTest Infected: Programmers Love Writing Tests A little test, a little code, a little test, a little code…
Using Ant to build J2EE Applications Kumar
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
The Design of JUnit Yonglei Tao. Test-First Development  An essential element in eXtreme Programming (XP)  Test is written before the code  As an executable.
Learning JUnit for Unit Testing JUnit Tutorial Dr. Robert L. Probert S.I.T.E., University of Ottawa Sept
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
1 Code Quality, Maintainability, Reusability, Debugging, Testing SIF8080, Sep. 27th 2001 Customer-driven project Carl-Fredrik Sørensen
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Testing With The JUnit Framwork Carl-Fredrik Sørensen, PhD Fellow
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSCE 2013L: Lab 1 Overview  Java Basics The JVM Anatomy of a Java Program  Object-Oriented Programming Overview  Example: Payroll.java JDK Tools and.
Ant Presentation by: Bart Taylor. What is Ant? The simple definition: A Java-based build tool The Official Definition: “Apache Ant is a Java-based build.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
CSC 480 Software Engineering Lecture 15 Oct 21, 2002.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 3 Understanding Ant datatypes and properties.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
JUnit Don Braffitt Updated: 10-Jun-2011.
JUnit A framework which provides hooks for easy testing of your Java code, as it's built Note: The examples from these slides can be found in ~kschmidt/public_html/CS265/Labs/Java/Junit.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
JUnit A Unit Testing Framework for Java. The Objective Introduce JUnit as a tool for Unit Testing Provide information on how to: Install it Build a test.
OOSC – Lab 1. Welcome Object Oriented programming is first and foremost a method for software construction, whose goal is to build high-quality software.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Topic: Junit Presenters: Govindaramanujam, Sama & Jansen, Erwin.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture 10 – Polymorphism Nancy Harris with additional slides Professor Adams from Lewis & Bernstein.
An Introduction to Ant. What is Ant? How do you use it? Why would you want to?
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
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.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Software Construction Lab 10 Unit Testing with JUnit
Computer Science 209 Testing With JUnit.
Java Programming Language
Test-driven development (TDD)
Object Oriented Programming in java
Chapter 11 Inheritance and Polymorphism Part 2
Learning JUnit for Unit Testing
JUnit Tutorial Hong Qing Yu Nov 2005.
Presentation transcript:

Testing and Build JUnit and Ant

JUnit Testing is not closely integrated with development. This prevents you from measuring the progress of development- you can't tell when something starts working or when something stops working. Using JUnit you can cheaply and incrementally build a test suite that will help you measure your progress, spot unintended side effects, and focus your development efforts.

Content The Problem Example Testing Practices

The problem Every programmer knows they should write tests for their code. Few do. This quickly becomes a vicious cycle- the more pressure you feel, the fewer tests you write. The fewer tests you write, the less productive you are and the less stable your code becomes. The less productive and accurate you are, the more pressure you feel. Breaking out requires an outside influence. The outside influence we needed in a simple testing framework that lets us do a little testing that makes a big difference.

Example Let's start simple and define a class Money to represent a value in a single currency. We represent the amount by a simple int. We represent a currency as a string holding the ISO three letter abbreviation (USD, CHF, etc.).

class Money { private int fAmount; private String fCurrency; public Money(int amount, String currency) { fAmount= amount; fCurrency= currency; } public int amount() { return fAmount; } public String currency() { return fCurrency; }

public Money add(Money m) { return new Money(amount()+m.amount(), currency()); } public boolean equals(Object anObject) { if (anObject instanceof Money) { Money aMoney= (Money) anObject; return aMoney.currency().equals(currency()) && amount() == aMoney.amount(); } return false; }

JUnit defines how to structure your test cases and provides the tools to run them. You implement a test in a subclass of TestCase. To test our Money implementation we therefore define MoneyTest as a subclass of TestCase. Put MoneyTest in the same package as the classes under test. In this way a test case has access to the package private methods. We add a test method testSimpleAdd, that will exercise the simple version of Money.add() above. A JUnit test method is an ordinary method without any parameters.

public class MoneyTest extends TestCase { //… public void testSimpleAdd() { Money m12CHF= new Money(12, "CHF"); // (1) Money m14CHF= new Money(14, "CHF"); Money expected= new Money(26, "CHF"); Money result= m12CHF.add(m14CHF); // (2) Assert.assertTrue(expected.equals(result)); // (3) } The testSimpleAdd() test case consists of: 1.Code which creates the objects we will interact with during the test. This testing context is commonly referred to as a test's fixture. All we need for the testSimpleAdd test are some Money objects. 2.Code which exercises the objects in the fixture. 3.Code which verifies the result.

A test for equals in MoneyTest. public void testEquals() { Money m12CHF= new Money(12, "CHF"); Money m14CHF= new Money(14, "CHF"); Assert.assertTrue(!m12CHF.equals(null)); Assert.assertEquals(m12CHF, m12CHF); Assert.assertEquals(m12CHF, new Money(12, "CHF")); // (1) Assert.assertTrue(!m12CHF.equals(m14CHF)); }

With JUnit you can have common fixture for running the tests by storing the fixture's objects in instance variables of your TestCase subclass and initialize them by overridding the setUp method. The symmetric operation to setUp is tearDown which you can override to clean up the test fixture at the end of a test. Each test runs in its own fixture and JUnit calls setUp and tearDown for each test so that there can be no side effects among test runs. public class MoneyTest extends TestCase { private Money f12CHF; private Money f14CHF; protected void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); }

We can rewrite the two test case methods, removing the common setup code: public void testEquals() { Assert.assertTrue(!f12CHF.equals(null)); Assert.assertEquals(f12CHF, f12CHF); Assert.assertEquals(f12CHF, new Money(12, "CHF")); Assert.assertTrue(!f12CHF.equals(f14CHF)); } public void testSimpleAdd() { Money expected= new Money(26, "CHF"); Money result= f12CHF.add(f14CHF); Assert.assertTrue(expected.equals(result)); }

Two additional steps are needed to run the two test cases: 1.define how to run an individual test case, 2.define how to run a test suite. JUnit supports two ways of running single tests: static dynamic In the static way you override the runTest method inherited from TestCase and call the desired test case. A convenient way to do this is with an anonymous inner class. Note that each test must be given a name to identify it if it fails. TestCase test= new MoneyTest("simple add") { public void runTest() { testSimpleAdd(); } }; A template method in the superclass will make sure runTest is executed when the time comes.

The dynamic way to create a test case to be run uses reflection to implement runTest. –It assumes the name of the test is the name of the test case method to invoke. –It dynamically finds and invokes the test method. To invoke the testSimpleAdd test we therefore construct a MoneyTest as shown below: TestCase test= new MoneyTest("testSimpleAdd"); The dynamic way is more compact to write but it is less static type safe. –An error in the name of the test case goes unnoticed until you run it and get a NoSuchMethodException.

As the last step to getting both test cases to run together, we have to define a test suite. In JUnit this requires the definition of a static method called suite. The suite method is like a main method that is specialized to run tests. Inside suite you add the tests to be run to a TestSuite object and return it. A TestSuite can run a collection of tests. TestSuite and TestCase both implement an interface called Test which defines the methods to run a test. This enables the creation of test suites by composing arbitrary TestCases and TestSuites. public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest(new MoneyTest("testEquals")); suite.addTest(new MoneyTest("testSimpleAdd")); return suite; }

there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically. public static Test suite() { return new TestSuite(MoneyTest.class); } Here is the corresponding code using the static way. public static Test suite() { TestSuite suite= new TestSuite(); suite.addTest( new MoneyTest("money equals") { protected void runTest() { testEquals(); } } ); suite.addTest( new MoneyTest("simple add") { protected void runTest() { testSimpleAdd(); } } ); return suite; }

JUnit comes with a graphical interface to run tests. Type the name of your test class in the field at the top of the window. Press the Run button. While the test is run JUnit shows its progress with a progress bar below the input field. The bar is initially green but turns into red as soon as there is an unsuccessful test. Failed tests are shown in a list at the bottom.

Testing practice "Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.“ What you want is to write tests that fail even though you think they should work, or tests that succeed even though you think they should fail. During Development- When you need to add new functionality to the system, write the tests first. Then, you will be done developing when the test runs. During Debugging- When someone discovers a defect in your code, first write a test that will succeed if the code is working. Then debug until the test succeeds.

Word of caution Once you get them running, make sure they stay running. There is a huge difference between having your suite running and having it broken. Ideally, you would run every test in your suite every time you change a method. Practically, your suite will soon grow too large to run all the time. Try to optimize your setup code so you can run all the tests. Or, at the very least, create special suites that contain all the tests that might possibly be affected by your current development. Then, run the suite every time you compile. And make sure you run every test at least once a day: overnight, during lunch

Ant Ant's buildfiles are written in XML. Each buildfile contains one project and at least one (default) target. Targets contain task elements. Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique.

Project A project has three attributes: –name the name of the project. –default the default target to use when no target is supplied. –basedir the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used.

Target Each project defines one or more targets. A target is a set of tasks you want to be executed. When starting Ant, you can select which target(s) you want to have executed. When no target is given, the project's default is used.

Target Ant tries to execute the targets in the depends attribute in the order they appear (from left to right). It is possible that a target can get executed earlier when an earlier target depends on it: Each target gets executed only once, even when more than one target depends on it.

AttributeDescriptionRequired namethe name of the target.yes dependsa comma-separated list of names of targets on which this target depends. No ifthe name of the property that must be set in order for this target to execute. No unlessthe name of the property that must not be set in order for this target to execute. No descriptiona short description of this target's function.no

Task A task is a piece of code that can be executed. A task can have multiple attributes (or arguments, if you prefer). The value of an attribute might contain references to a property. These references will be resolved before the task is executed. Tasks have a common structure: where name is the name of the task, attributeN is the attribute name, and valueN is the value for this attribute. Tasks can be assigned an id attribute: where taskname is the name of the task, and taskID is a unique identifier for this task. You can refer to the corresponding task object in scripts or other tasks via this name.

Properties A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant. A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between " ${ " and " } " in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.

Built-in properties Ant provides access to all system properties as if they had been defined using a task. For example, ${os.name} expands to the name of the operating system. In addition, Ant has some built-in properties: –basedir the absolute path of the project's basedir (as set with the basedir attribute of ). –ant.file the absolute path of the buildfile. –ant.version the version of Ant ant.project.name the name of the project that is currently executing; it is set in the name attribute of. –ant.java.version the JVM version Ant detected; currently it can hold the values "1.1", "1.2", "1.3", "1.4" and "1.5".

simple example build file

Path-like Structures You can specify PATH - and CLASSPATH -type references using both " : " and " ; " as separator characters. Ant will convert the separator to the correct character of the current operating system. Wherever path-like values need to be specified, a nested element can be used. This takes the general form of: The location attribute specifies a single file or directory relative to the project's base directory (or an absolute filename), while the path attribute accepts colon- or semicolon-separated lists of locations.

In addition, DirSets, FileSets, and FileLists can be specified via nested,, and elements, respectively.

If you want to use the same path-like structure for several tasks, you can define them with a element at the same level as targets, and reference them via their id attribute. A path-like structure can include a reference to another path-like structure via nested elements:

References Any project element can be assigned an identifier using its id attribute. In most cases the element can subsequently be referenced by specifying the refid attribute on an element of the same type. This can be useful if you are going to replicate the same snippet of XML over and over again--using a structure more than once.