Junit At the forefront of Test Driven Development.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Unit Testing Australian Development Centre Brisbane, Australia.
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.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Test-Driven Development and Refactoring CPSC 315 – Programming Studio.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Objectives: Test Options JUnit Testing Framework TestRunners Test Cases and Test Suites Test Fixtures JUnit.
Approach of Unit testing with the help of JUnit Satish Mishra
Test-Driven Development and Refactoring Project 3 Lecture 1 CPSC 315 – Programming Studio Fall 2009.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Introduction to Eclipse, Unit Testing and JUnit David Rabinowitz.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 13 Inheritance and Polymorphism Animated Version.
JUnit, Revisited 17-Apr-17.
Advanced Object-Oriented Programming Features
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Presentation Outline What is JUnit? Why Use JUnit? JUnit Features Design of JUnit Downloading JUnit Writing Tests – TestCase – TestSuite Organizing The.
Unit Testing Discussion C. Unit Test ● public Method is smallest unit of code ● Input/output transformation ● Test if the method does what it claims ●
George Blank University Lecturer. JUnit for Test Driven Development By Vivek Bhagat, George Blank.
JsUnit An introduction to unit testing in-browser JavaScript with JsUnit Edward Hieatt February, 2005 JsUnit.
Programmer Testing Testing all things Java using JUnit and extensions.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
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
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.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Test Driven Development Arrange, Act, Assert… Awesome Jason Offutt Software Engineer Central Christian Church
Software Engineering 1 Object-oriented Analysis and Design Chap 21 Test-Driven Development and Refactoring.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Inheritance in the Java programming language J. W. Rider.
Refactoring for Testability (or how I learned to stop worrying and love failing tests) Presented by Aaron Evans.
Introduction to JUnit 3.8 SEG 3203 Winter ‘07 Prepared By Samia Niamatullah.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Refactoring & Testability. Testing in OOP programming No life in flexible methodologies and for refactoring- infected developers without SOME kind of.
Simple Java Unit Testing with JUnit 4 and Netbeans 6.1 Kiki Ahmadi JUG-Bonek.
Programming with Assertions © Allan C. Milne v
EMBEDDED REAL-TIME, INC. December 8, 2015 Java Unit Mark Mosher Rochester Java Users Group.
JUnit Eclipse, Java and introduction to Junit. Topics Covered  Using Eclipse IDE  Example Java Programs  Junit Introduction.
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.
Software testing techniques Software testing techniques Software Testability Presentation on the seminar Kaunas University of Technology.
Testing OO software. State Based Testing State machine: implementation-independent specification (model) of the dynamic behaviour of the system State:
1 Presentation Title Test-driven development (TDD) Overview David Wu.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Inheritance and Polymorphism.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Introduction to Unit Testing and JUnit David Rabinowitz.
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.
Dept of Computer Science University of Maryland College Park
Logger, Assert and Invariants
Introduction to JUnit CS 4501 / 6501 Software Testing
Computer Science 209 Testing With JUnit.
Test-driven development (TDD)
Introduction to JUnit CS 4501 / 6501 Software Testing
Chapter 5 Classes.
Presentation transcript:

Junit At the forefront of Test Driven Development

Some Distinctions Unit Testing –Hand testing of components Automated Unit Testing –Automatic component testing run every build Integration Testing –Hand testing a component based system Automated Integration Testing –Automatic system integration and testing following successful Unit testing

Motivations for Unit Testing Analgous to Light Manufacturing –enforce component quailty and stop the assembly line (at compilation) rather than testing for quality at the assembly (integration) stage Higher component quality, lower bug count Higher component quality leads to reduced cycle times This leads to a higher rate of adaptability to changing requirements

Motivations for Automated Unit Testing Efficiency –automated testing defeats developer excuses to skip writing and running tests Code Quality –Incorporating automated unit testing into the build mitigates checkin of poor code Design Quality –Bold refactorings lead to modular code –Modular code is more adaptive to change Confidence –a test suite gives instant feedback to developers making changes, allowing bold refactorings

Using Junit from Ant Make sure junit.jar is in your classpath Taskdef Junit tag and test tags

Writing a Junit Test Create your class Use a method of your class Use Assertions to test the output of the method –junit.framework.Assert Assert.assertTrue( String, boolean ) Assert.assertNull( String, Object ) Assert.assertEquals( String, Object, Object ) –Assertions print out error messages

Creating a Junit Test Suite Tests typically extend TestSuite TestSuite follows the Composite pattern –TestSuites aggregate other test suites Usually one suite per java package –com.doofus.framework Test*.java frameworkSuite.java Include tests by adding them to the suite –Suite.add( new FooTest(“TestCreate”) );

Designing a Unit Test Be aware of the issues distinguishing various types of tests – good overviewhttp://jakarta.apache.org/cactus/ Code Logic Testing –out of application context Integration Unit Testing –inside application context Functional Unit Testing –testing from outside your application

What resources do your classes need to be tested? What level of coupling does your design indicate? –Inability to decouple classes leads right to integration level testing –Ability to decouple classes leads to unit testing –Design your code to be unit tested Unit Testing limits the scope of testing –to the component being tested, –not integration with other components –substitute dependencies with mock objects Integration Testing tests features using all integrated components

Unit Testing a Class with Mock Objects Classes not requiring an application context can be unit-tested easily –Static utility methods, basic entity classes lacking control features Interaction with other classes should be against mock objects –Test a control class against a mock entity class Application classes that require other application classes can be contextualized in a mock application or with mock application classes

Mock Objects Mock objects respect an interface –Implement a business interface –Extend a business class but nullify it’s logic Mock objects often track their state with flags –You pass a mock entity into a controller, stimulate the controller, and test to see that variables have changed inside the mock object Mock objects can verify their own state and might have a verify() method –Possibility of many mock implementations of a business class, possilby one per test, if the verify() methods are polymorphic

Mock Objects and Decoupling Be careful with that “new” statement in your business classes –You don’t want to test other classes while testing this one –new can only select one implementation Alternatives –Mock extension of business implementations –Inversion of control

Generation of mock objects Invoking Mock object class generators are a worthwhile step in your build automation process –EasyMock –MockMaker Hand tune and refactor mocks as necessary

Factory methods (protected) Create a mock object as a subclass of your production implementation –Invoke a mock extension of your business class during testing –All new statements are refactored into protected factory methods that are overridden by mock implementations –Mock factory methods return mock objects

Before Factory Method Refactoring public class AuthToken { public void authenticate() throws SecurityVetoException; } public class UserManager { public AuthToken authenticate( String login, String passwd ) { AuthToken a = new AuthToken( login, passwd ); a.authenticate(); Return a }

Refactored with Factory method interface AuthToken { … } public class UserToken implements AuthToken { … } public class UserManager { public AuthToken authenticate( String login, String passwd ) { AuthToken token = createAuthToken(login, passwd); token..authenticate(); return token; } protected AuthToken createAuthToken( String login, String passwd ) { return new UserToken( login, passwd ); }

Mock Extends Business Impl public class MockUserManager extends UserManager { private classs MockAuthToken extends UserToken { boolean m_authFlag = false; public void authenticate() { m_authFlag = true; } public void validate() { Assert.assertNotNull( m_login ); Assert.assertNotNull( m_passwd ); Assert.assertTrue( m_authFlag ); } protected AuthToken createAuthToken( String login, String passwd() { return new MockAuthToken( login, passwd ); }

UnitTest uses extending class public class UserManagerTest extends TestSuite { public UserManagerTest() { super(“UserManagerTest”); suite.add( new UserManagerTest(“testAuthentication”) ); } public void testAuthentication() { UserManager u = new MockUserManager(); MockAuthToken mock = (MockAuthToken) u.authenticate( “Santa”, “Claus” ); mock.validate(); //throws X if invalid, test fails }

Inversion of control classes and components rely on composition rather than extension Reduces use of inheritance Components do not say “new”, rather contextualization provides resources Jakarta Avalon is a framework for developing Inversion of Control components Works well with JNDI

Example of Inversion of Control public class UserManager extends AbstractResourceDependent { private AuthTokenFactory m_authFactory = null; public UserManager() { super(); } public contextualize( Context c ) { super.contextualize( c ); m_authFactory = (AuthTokenFactory) c.get( AuthTokenFactory.HINT ); } public AuthToken authenticate( String login, String passwd ) { AuthToken token = m_authFactory.create( login, passwd ); return token; }

Example of IOC Unit Test Public class UserManagerTest extends TestSuite { public UserManagerTest() { super(“UserManagerTest”); suite.add( new UserManagerTest(“testAuthentication”) ); } public void testAuthentication() { Resources r = new Resources(); r.put( AuthTokenFactory.HINT, new MockTokenFactory() ); UserManager u = new UserManager(); u.contextualize( r ); MockAuthToken mock = (MockAuthToken) u.authenticate( “Santa”, “Claus” ); mock.validate(); //throws X if invalid, test fails }

More about Junit Tests TestSuites Assertions

Integration Testing a feature Integration testing tests one feature of an integrated application Uses the application context rather than a mock environment Cactus is a Jakarta project revolving around integration testing web applications

Conclusion Junit, Ant provide a framework for automated testing Automated testing of classes focuses debugging effort Junit can be used for application integration testing as well