Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing.

Slides:



Advertisements
Similar presentations
A Taste of Visual Studio 2005 David Grey. Introduction In this session we will introduce Visual Studio 2005 and its features and examine those features.
Advertisements

Acceptance Testing vs. Unit Testing: A Developers Perspective Owen Rogers Rob Styles
Apache Struts Technology
Test-Driven Development José Almeida, Microsoft
HP Quality Center Overview.
Key-word Driven Automation Framework Shiva Kumar Soumya Dalvi May 25, 2007.
Unit and Functional Testing with JUnit and Related Tools Greg Barnes University of Washington
Copyright © 2006 Korson-Consulting 1/219 Unit 4 Test First Development.
23-Jun-15 Unit Testing in Ruby. Programming methodologies The grim facts: The majority of large programming projects fail Projects that succeed are usually.
Testing HCI Usability Testing. Chronological order of testing Individual program units are built and tested (white-box testing / unit testing) Units are.
From Module Breakdown to Interface Specifications Completing the architectural design of Map Schematizer.
Using TDD Making code maintainable, reusable and readable by writing tests.
Spring Dynamic Modules. Startlocation: Documentation: /1.2.1/reference/html/
Test-Driven Development “Test first, develop later!” –OCUnit.
Unit Testing Tips and Tricks: Database Interaction Louis Thomas.
Stateful Web Application Development with Spring Web Flow John Case Senior Consultant Centare Group, Ltd.
JUnit testing and Spring How to benefit from Spring support for jUnit during development.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
QWise software engineering – refactored! Testing, testing A first-look at the new testing capabilities in Visual Studio 2010 Mathias Olausson.
Advanced Shell Programming. 2 Objectives Use techniques to ensure a script is employing the correct shell Set the default shell Configure Bash login and.
System Implementation. System Implementation and Seven major activities Coding Testing Installation Documentation Training Support Purpose To convert.
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
Model Bank Testing Accelerators “Ready-to-use” test scenarios to reduce effort, time and money.
Unit and Functional Testing Your Flex Applications Mike Nimer Dir. Of Engineering nomee.com.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Understanding the Bean Lifecycle An.
Junit At the forefront of Test Driven Development.
Module 6: Configuring User Environments Using Group Policy.
Dr. Tom WayCSC Testing and Test-Driven Development CSC 4700 Software Engineering Based on Sommerville slides.
First BlueJ Day Houston, 2006 Unit Testing with BlueJ Bruce Quig Deakin University.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
A Practical Guide To Unit Testing John E. Boal TestDrivenDeveloper.com.
Copyright 2008 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Module Name Subtitle.
TEST-1 6. Testing & Refactoring. TEST-2 How we create classes? We think about what a class must do We focus on its implementation We write fields We write.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Forms with Spring MVC Handling Form.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
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.
Mock objects.
Confidential Continuous Integration Framework (CIF) 5/18/2004.
Test-Driven Development Eduard Miric ă. The problem.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Software testing techniques Software testing techniques Software Testability Presentation on the seminar Kaunas University of Technology.
1 Presentation Title Test-driven development (TDD) Overview David Wu.
A Quick CCM Example. Tutorial on CCM Introduction A Simple Sender&Receiver Scenario Sender sends out the click-out event to inform the Receiver. Receiver.
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
Visual Basic for Application - Microsoft Access 2003 Finishing the application.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
(1) Test Driven Development Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Unit, Regression, and Behavioral Testing Based On: Unit Testing with JUnit and CUnit by Beth Kirby Dec 13, 2002 Jules.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Overview of the Spring Framework Introducing.
Refactoring and Integration Testing or Strategy, introduced reliably by TDD The power of automated tests.
Automated Testing in Sakai Testing applications and services in isolation and in context Josh Holtzman, UC Berkeley David Haines, University of Michigan.
Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring JDBC Simplifying.
Testing Your Alfresco Add-ons Michael Suzuki Software Engineer.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Testing PA165 Dec 9, 2014 Petr Adámek, Tomáš Pitner.
Automated Testing with PHPUnit. How do you know your code works?
Test-driven development
Introduction to 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 Testing.
Systematic Manual Testing
History, Characteristics and Frameworks
Test-driven development (TDD)
Testing and Test-Driven Development CSC 4700 Software Engineering
In the Senior Design Center
Open Source Tool Based Automation solution with Continuous Integration and end to end BDD Implementation Arun Krishnan - Automation Manager Maria Afzal-
Open Source Tool Based Automation solution with Continuous Integration and end to end BDD Implementation Arun Krishnan - Automation Manager Maria Afzal-
Presentation transcript:

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Testing Spring Applications Unit Testing without Spring, and Integration Testing with Spring

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 2 Topics in this session Test Driven Development Unit Testing vs. Integration Testing Unit Testing with Stubs Unit Testing with Mocks Integration Testing with Spring

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 3 Test Driven Development Unit Testing vs. Integration Testing Unit Testing with Stubs Unit Testing with Mocks Integration Testing with Spring Topics in this session

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 4 What is TDD TDD = Test Driven Development Is it writing tests before the code? Is it writing tests at the same time as the code? That is not what is most important Do not get hung up on terminology Ultimately, TDD is about writing automated tests that verify code actually works

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 5 “But I Don’t Have Time to Write Tests!” Every development process includes testing Either automated or manual Automated tests result in a faster development cycle overall Compare the time required to run a test in your IDE (seconds) vs. the time required for someone to manually follow a script (typically minutes/hours)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 6 TDD and Agility Comprehensive test coverage provides confidence Confidence enables refactoring Refactoring is essential to agile development

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 7 TDD and Design Testing makes you think about your design If your code is hard to test then the design should be reconsidered

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 8 Benefits of Continuous Integration The cost to fix a bug grows exponentially in proportion to the time before it is discovered Continuous Integration (CI) focuses on reducing the time before the bug is discovered Effective CI requires automated tests time to fix time until discovered

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 9 Test Driven Development Unit Testing vs. Integration Testing Unit Testing with Stubs Unit Testing with Mocks Integration Testing with Spring Topics in this session

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 10 Unit Testing vs. Integration Testing Unit Testing –Tests one unit of functionality –Keeps dependencies minimal –Isolated from the environment (including Spring) Integration Testing –Tests the interaction of multiple units working together –Integrates infrastructure

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 11 Unit Testing Verify a unit works in isolation –If A depends on B, A’s tests should not fail because of a bug in B –A’s test should not pass because of a bug in B Stub or mock out dependencies if needed Have each test exercise a single scenario A path through the unit

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 12 Example Unit to be Tested public class AuthenticatorImpl implements Authenticator { private AccountDao accountDao; public AuthenticatorImpl(AccountDao accountDao) { this.accountDao = accountDao; } public boolean authenticate(String username, String password) { Account account = accountDao.getAccount(username); if (account.getPassword().equals(password)) { return true; } else { return false; } public class AuthenticatorImpl implements Authenticator { private AccountDao accountDao; public AuthenticatorImpl(AccountDao accountDao) { this.accountDao = accountDao; } public boolean authenticate(String username, String password) { Account account = accountDao.getAccount(username); if (account.getPassword().equals(password)) { return true; } else { return false; } External dependencyUnit business logic (2 paths)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 13 Test Driven Development Unit Testing vs. Integration Testing Unit Testing with Stubs Unit Testing with Mocks Integration Testing with Spring Topics in this session

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 14 Implementing a Stub class StubAccountDao implements AccountDao { private Map accountsByUsername = new HashMap(); public StubAccountDao() { accountsByUsername.put(“lisa”, new Account(“lisa”, “secret”)); } public Account getAccount(String user) { return (Account) accountsByUsername.get(user); } class StubAccountDao implements AccountDao { private Map accountsByUsername = new HashMap(); public StubAccountDao() { accountsByUsername.put(“lisa”, new Account(“lisa”, “secret”)); } public Account getAccount(String user) { return (Account) accountsByUsername.get(user); } Sets up state for testing

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 15 Testing with a Stub public class AuthenticatorImplTests extends TestCase { private AuthenticatorImpl authenticator; public void setUp() { authenticator = new AuthenticatorImpl(new StubAccountDao()); } public void testSuccessfulAuthentication() { assertTrue(authenticator.authenticate(“lisa”, “secret”)); } public void testInvalidPassword() { assertFalse(authenticator.authenticate(“lisa”, “invalid”)); } public class AuthenticatorImplTests extends TestCase { private AuthenticatorImpl authenticator; public void setUp() { authenticator = new AuthenticatorImpl(new StubAccountDao()); } public void testSuccessfulAuthentication() { assertTrue(authenticator.authenticate(“lisa”, “secret”)); } public void testInvalidPassword() { assertFalse(authenticator.authenticate(“lisa”, “invalid”)); } Stub is configured Scenarios are exercised based on the state of stub

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 16 Stub Considerations Advantages Easy to implement and understand Reusable Disadvantages A change to the interface requires the stub to be updated Your stub must implement all methods, even those not used by a specific scenario

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 17 Test Driven Development Unit Testing vs. Integration Testing Unit Testing with Stubs Unit Testing with Mocks Integration Testing with Spring Topics in this session

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 18 Steps to Testing with a Mock 1.Use a mocking library to generate a mock object Implements the dependent interface on-the-fly 2.Record the mock with expectations of how it will be used for a scenario What methods will be called What values to return 3.Exercise the scenario 4.Verify mock expectations were met

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 19 The Easy Mock Library EasyMock 2.0 is best mocking library available Used extensively in Spring for unit testing What is taught in this course

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 20 Example - Testing with EasyMock On setup 1.Create mock(s) 2.Create unit For each test 1.Record expected method calls and set mock return values 2.Call replay 3.Exercise test scenario 4.Call verify public class AuthenticatorImplTests extends TestCase { private AuthenticatorImpl authenticator; private AccountDao accountDao; protected void setUp() { accountDao = EasyMock.createMock(AccountDao.class); authenticator = new AuthenticatorImpl(accountDao); } public void testValidUserWithCorrectPassword() { EasyMock.expect(accountDao.getAccount(“lisa”)). andReturn(new Account(“lisa”, “secret”)); EasyMock.replay(accountDao); assertTrue(authenticator.authenticate(“lisa”, “secret”)); EasyMock.verify(accountDao); } public class AuthenticatorImplTests extends TestCase { private AuthenticatorImpl authenticator; private AccountDao accountDao; protected void setUp() { accountDao = EasyMock.createMock(AccountDao.class); authenticator = new AuthenticatorImpl(accountDao); } public void testValidUserWithCorrectPassword() { EasyMock.expect(accountDao.getAccount(“lisa”)). andReturn(new Account(“lisa”, “secret”)); EasyMock.replay(accountDao); assertTrue(authenticator.authenticate(“lisa”, “secret”)); EasyMock.verify(accountDao); } }

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 21 Mock Considerations Advantages No additional class to maintain You only need to setup what is necessary for the scenario you are testing Disadvantages Harder to understand Changing unit internals often breaks your tests

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 22 Mocks or Stubs? You will probably use both General recommendations Favor stubs in most situations Use mocks when stubs are impractical Always consider the specific situation Read “Mocks Aren’t Stubs” by Martin Fowler

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 23 Test Driven Development Unit Testing vs. Integration Testing Unit Testing with Stubs Unit Testing with Mocks Integration Testing with Spring Topics in this session

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 24 Integration Testing Tests the interaction of multiple units Tests application classes in the context of their surrounding infrastructure Infrastructure may be “scaled down”

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 25 Located in spring-mock.jar Consists of several JUnit test support classes Central support class is AbstractDependencyInjectionSpringContextTests Extends JUnit TestCase Loads system test configuration from one or more XML bean definition files Caches a shared ApplicationContext across multiple test methods Resolves test dependencies Spring’s Integration Test Support

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 26 Using Spring’s Integration Test Support public final class AuthenticatorTests extends AbstractDependencyInjectionSpringContextTests { private Authenticator protected String[] getConfigLocations() { return new String[] { “classpath:system-test-config.xml” }; } public void setAuthenticator(Authenticator authenticator) { this.authenticator = authenticator; } public void testSuccessfulAuthentication() { authenticator.authenticate(...);... } public final class AuthenticatorTests extends AbstractDependencyInjectionSpringContextTests { private Authenticator protected String[] getConfigLocations() { return new String[] { “classpath:system-test-config.xml” }; } public void setAuthenticator(Authenticator authenticator) { this.authenticator = authenticator; } public void testSuccessfulAuthentication() { authenticator.authenticate(...);... } Extend support class Point to system test configuration file Define a dependency on the system entry pointTest the system as normal

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 27 JUnit Test Runner AuthenticatorTests ClassPathXmlApplicationContext setUp (first time only) getConfigLocations() new(configLocations) Assigned to the protected field named applicationContext setAuthenticator(Authenticator) autowireBeanProperties(this) Calls each setter the context can satisfy by type Test Case Setup (first time only)

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 28 Benefits of Integration Testing with Spring No need to deploy to an external container to test application functionality Run everything quickly inside your IDE Allows reuse of your configuration between test and production environments Application configuration logic is typically reused Infrastructure configuration is environment-specific DataSources JMS Queues

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. 29 Summary Testing is an essential part of any development Unit testing tests a class in isolation where external dependencies should be minimized Consider creating stubs or mocks to unit test You don’t need Spring to unit test Integration testing tests the interaction of multiple units working together Spring provides good integration testing support

Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. LAB Testing Spring Applications