SlideShare ist ein Scribd-Unternehmen logo
1 von 52
Downloaden Sie, um offline zu lesen
Java Test Automation
  Real-world patterns and practices




          Dmitry Buzdin
TESTING
AUTOMATION
Wat iz test automation?

 Tests are automated!

 Tests could be run on developer laptop

 Tests are running in CI on regular basis

 Tests do not require network access
Signs that there is not
  enough automation
 Starting GUI to check backend changes

 Deploying to server to check functionality

 Relying on debug logging

 Testing is for testers

 You do not sleep well
Testing patterns in this
presentation are applicable
 to any framework/system
SAMPLE APPLICATION
Typical Layers
     User Interface

  Application Services

     Domain Model

Integration Components

External Services & Storage
Our Requirements

Display weather forecasts
Store them in database
Lots of other usual requirements
 Cloud, HTML5, BigData...
Web Client

          REST API

         Service Layer

         Domain Model

 ORM         Integration Layer

          Weather        Other
SQL DB
          REST API       APIs
Technology Stack

Front-end - Twitter Bootstrap + jQuery

REST - JAX-RS 1.1 / Jersey

Dependency Injection - CDI / Weld

ORM - JPA 2.0 / Hibernate

SQL DB - PostgreSQL
Source of weather data - REST API




http://api.openweathermap.org/data/2.1/find/name?
                      q=riga
DEMO
TESTING STRATEGY
Unit Tests

Integration Tests

Functional Tests
UNIT TESTING
Unit Tests

Classes are tested in isolation
(almost)
One test method checks one
use-case
One class <-> one test class
Web Client

          REST API

         Service Layer

         Domain Model

 ORM         Integration Layer

          Weather        Other
SQL DB
          REST API       APIs
JUnit Tests
@Before
public void setUp() {
   // Preparing object for test
}

@Test
public   void shouldFindWeatherByCity() throws Exception {
    //   Conditions set-up
    //   Method under test invocation
    //   Assertions
}




                     http://junit.org/
Use Dependency Injection
  Separation of classes

  Mostly about testing

  Popular DI frameworks

    Spring

    CDI

    Guice
Injection Types


by constructor
by setter
by field
Mockito Mocks
// Mocking all class dependencies
service = new WeatherServiceImpl();
service.weatherSource = Mockito.mock(WeatherSource.class);

// Sets-up mock reaction
when(service.weatherSource.findByCityName(eq("Kolka")))
  .thenReturn(expectedResult);

// Your method under test here

// Verifies interactions with mock
verify(service.entityManager)
  .persist(any(Temperature.class));



          http://code.google.com/p/mockito/
Hamcrest Matchers

assertThat(ages, everyItem(greaterThan(18)));

assertThat(param, equalTo(42));

assertThat(param, notNullValue());

assertThat(object, is(String.class))

assertThat(object, anyOf(is(String.class), is(Integer.class))




       http://code.google.com/p/hamcrest/
Benefits

Forget assertEquals() !

Hamcrest matchers are

  expressive

  flexible

  extendable
DEMO
INTEGRATION
  TESTING
Integration Tests

Should not start the whole application

Testing integration components

  Remote API calls

  Data conversion

  Fault-scenarios
Web Client

          REST API

         Service Layer

         Domain Model

 ORM         Integration Layer

          Weather        Other
SQL DB
          REST API       APIs
Fake Dependencies

Write code for emulating dependencies

Fake Web Service

Fake FTP server

Fake InputStream

Fake XML response
DEMO
Persistence Tests


Isolate and test all persistence
operations

Ideally all CRUD operations

Could be done in Generic way
In-memory Storage
Transient database for test execution

Some DBs have it built-in

For SQL DB mocking pick H2

  Fast

  Emulation modes


   http://www.h2database.com/
DEMO
FUNCTIONAL
  TESTING
Functional Tests

Not testing UI
Starting application context
Emulating multiple user requests
Persisting intermediate results
Web Client

          REST API

         Service Layer

         Domain Model

 ORM         Integration Layer

          Weather        Other
SQL DB
          REST API       APIs
Embedded Container

Test should start application
It is possible to start embedded
  CDI Container
  EJB Container
  Spring Container
Functional Testing using
    Service Layer

 Ignoring GUI data conversion
 Easier to test
 Faster test execution
Mock Integrations

Could be replaced using
 Properties and factory beans
 Spring @Profile
 CDI @Alternative
 Guice Modules
Environment Switch
Application should run in several modes

Done with System environment variable

Typical modes:

  production

  local deployment

  embedded testing
Configuration Override


Environments overrides settings
Hierarchical configurations
Properties, YAML or other
Initial Data

Reset database after every test and
insert initial data

Reuse ORM mapping or other persistence
layer

Or use specialized tools like DbUnit
API-Level Testing

Write test for your API
Some examples:
 SOAP
 REST
 EJB
DEMO
Web Client

          REST API

         Service Layer

         Domain Model

 ORM         Integration Layer

          Weather        Other
SQL DB
          REST API       APIs
Embedded Web Server


It is possible to run embedded
 Jetty
 Glassfish/Grizzly
JUnit Rules

@Rule
public EmbeddedJetty jetty = new
EmbeddedJetty();

@Test
public void shouldTestEmbeddedJetty() {
    ...
}
DEMO
SUMMARY
Why is it all needed?


Decrease cost of change
Increase software quality
Remove fear of making changes
When Testing is Enough?


  If CI build passes it is
     safe to deploy to
        production
What was not covered?

 Automated
  Acceptance testing
  GUI-level testing
  Performance testing
Dmitry Buzdin
Freelance Software Architect

       http://buzdin.lv
     buzdin@gmail.com
          @buzdin

Weitere ähnliche Inhalte

Was ist angesagt?

Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
Maveryx
 
Testing with test_complete
Testing with test_completeTesting with test_complete
Testing with test_complete
binuiweb
 

Was ist angesagt? (20)

Open Source Software Testing Tools
Open Source Software Testing ToolsOpen Source Software Testing Tools
Open Source Software Testing Tools
 
Keyword-driven Test Automation Framework
Keyword-driven Test Automation FrameworkKeyword-driven Test Automation Framework
Keyword-driven Test Automation Framework
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework Designs
 
Test automation principles, terminologies and implementations
Test automation principles, terminologies and implementationsTest automation principles, terminologies and implementations
Test automation principles, terminologies and implementations
 
Real world selenium resume which gets more job interviews
Real world selenium resume which gets more job interviewsReal world selenium resume which gets more job interviews
Real world selenium resume which gets more job interviews
 
Mastering Test Automation: How to Use Selenium Successfully
Mastering Test Automation: How to Use Selenium Successfully Mastering Test Automation: How to Use Selenium Successfully
Mastering Test Automation: How to Use Selenium Successfully
 
Test automation framework
Test automation frameworkTest automation framework
Test automation framework
 
Testing tools concepts
Testing tools conceptsTesting tools concepts
Testing tools concepts
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
 
Build Your Custom Performance Testing Framework
Build Your Custom Performance Testing FrameworkBuild Your Custom Performance Testing Framework
Build Your Custom Performance Testing Framework
 
Testing with test_complete
Testing with test_completeTesting with test_complete
Testing with test_complete
 
SoftTest Ireland: Model Based Testing - January 27th 2011
SoftTest Ireland: Model Based Testing - January 27th 2011SoftTest Ireland: Model Based Testing - January 27th 2011
SoftTest Ireland: Model Based Testing - January 27th 2011
 
Laws of test automation framework
Laws of test automation frameworkLaws of test automation framework
Laws of test automation framework
 
Software Automation Testing Introduction
Software Automation Testing IntroductionSoftware Automation Testing Introduction
Software Automation Testing Introduction
 
Web UI test automation instruments
Web UI test automation instrumentsWeb UI test automation instruments
Web UI test automation instruments
 
Test Automation Demonstration with Dr Yongyan Wang by XBOSoft
Test Automation Demonstration with Dr Yongyan Wang by XBOSoftTest Automation Demonstration with Dr Yongyan Wang by XBOSoft
Test Automation Demonstration with Dr Yongyan Wang by XBOSoft
 
Selenium Tutorial for Beginners | Automation framework Basics
Selenium Tutorial for Beginners | Automation framework BasicsSelenium Tutorial for Beginners | Automation framework Basics
Selenium Tutorial for Beginners | Automation framework Basics
 
Software testing tools
Software testing toolsSoftware testing tools
Software testing tools
 
Hybrid Automation Framework Development introduction
Hybrid Automation Framework Development introductionHybrid Automation Framework Development introduction
Hybrid Automation Framework Development introduction
 
Test Complete
Test CompleteTest Complete
Test Complete
 

Andere mochten auch

Codingstandards matiar
Codingstandards matiarCodingstandards matiar
Codingstandards matiar
Matiar Rahman
 
Automation Benefits and its future
Automation Benefits and its futureAutomation Benefits and its future
Automation Benefits and its future
RIA RUI Society
 

Andere mochten auch (20)

Presentation on automation
Presentation on automationPresentation on automation
Presentation on automation
 
A Pragmatic Introduction to Unit Testing
A Pragmatic Introduction to Unit TestingA Pragmatic Introduction to Unit Testing
A Pragmatic Introduction to Unit Testing
 
The Benefits of Automation - Digiday Programmatic Rome, 11/10/15
The Benefits of Automation - Digiday Programmatic Rome, 11/10/15The Benefits of Automation - Digiday Programmatic Rome, 11/10/15
The Benefits of Automation - Digiday Programmatic Rome, 11/10/15
 
Codingstandards matiar
Codingstandards matiarCodingstandards matiar
Codingstandards matiar
 
Oojeema - SSC JPIA Benefits of Automation
Oojeema - SSC JPIA Benefits of AutomationOojeema - SSC JPIA Benefits of Automation
Oojeema - SSC JPIA Benefits of Automation
 
Automating Web Application Security Testing With OWASP ZAP DOT NET API - Tech...
Automating Web Application Security Testing With OWASP ZAP DOT NET API - Tech...Automating Web Application Security Testing With OWASP ZAP DOT NET API - Tech...
Automating Web Application Security Testing With OWASP ZAP DOT NET API - Tech...
 
Automation Benefits and its future
Automation Benefits and its futureAutomation Benefits and its future
Automation Benefits and its future
 
Automation presentation
Automation presentationAutomation presentation
Automation presentation
 
Andy Davidson Automation Presentation from UKNOF 31
Andy Davidson Automation Presentation from UKNOF 31Andy Davidson Automation Presentation from UKNOF 31
Andy Davidson Automation Presentation from UKNOF 31
 
Automation testing API in Java
Automation testing API in JavaAutomation testing API in Java
Automation testing API in Java
 
Deploy and Destroy Complete Test Environments
Deploy and Destroy Complete Test EnvironmentsDeploy and Destroy Complete Test Environments
Deploy and Destroy Complete Test Environments
 
2015-StarWest presentation on REST-assured
2015-StarWest presentation on REST-assured2015-StarWest presentation on REST-assured
2015-StarWest presentation on REST-assured
 
Api testing
Api testingApi testing
Api testing
 
4 Major Advantages of API Testing
4 Major Advantages of API Testing4 Major Advantages of API Testing
4 Major Advantages of API Testing
 
API Testing: The heart of functional testing" with Bj Rollison
API Testing: The heart of functional testing" with Bj RollisonAPI Testing: The heart of functional testing" with Bj Rollison
API Testing: The heart of functional testing" with Bj Rollison
 
Control system
Control systemControl system
Control system
 
Api testing
Api testingApi testing
Api testing
 
How to Automate API Testing
How to Automate API TestingHow to Automate API Testing
How to Automate API Testing
 
Control Systems notes
Control Systems notesControl Systems notes
Control Systems notes
 
control system
control systemcontrol system
control system
 

Ähnlich wie Pragmatic Java Test Automation

Test strategy for web development
Test strategy for web developmentTest strategy for web development
Test strategy for web development
alice yang
 

Ähnlich wie Pragmatic Java Test Automation (20)

Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Test strategy for web development
Test strategy for web developmentTest strategy for web development
Test strategy for web development
 
Bdd test automation analysis
Bdd test automation analysisBdd test automation analysis
Bdd test automation analysis
 
Testing your application on Google App Engine
Testing your application on Google App EngineTesting your application on Google App Engine
Testing your application on Google App Engine
 
Testing Your Application On Google App Engine
Testing Your Application On Google App EngineTesting Your Application On Google App Engine
Testing Your Application On Google App Engine
 
Overview of Lab Management and TFS
Overview of Lab Management and TFSOverview of Lab Management and TFS
Overview of Lab Management and TFS
 
JSS build and deployment
JSS build and deploymentJSS build and deployment
JSS build and deployment
 
Testing distributed systems in production
Testing distributed systems in productionTesting distributed systems in production
Testing distributed systems in production
 
BDD/TDD based automation on short-term project on real example, Igor Kokoz
BDD/TDD based automation on short-term project on real example, Igor Kokoz BDD/TDD based automation on short-term project on real example, Igor Kokoz
BDD/TDD based automation on short-term project on real example, Igor Kokoz
 
Unit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJSUnit Testing and Coverage for AngularJS
Unit Testing and Coverage for AngularJS
 
Testing for fun in production Into The Box 2018
Testing for fun in production Into The Box 2018Testing for fun in production Into The Box 2018
Testing for fun in production Into The Box 2018
 
Grails 1.1 Testing - Unit, Integration & Functional
Grails 1.1 Testing - Unit, Integration & FunctionalGrails 1.1 Testing - Unit, Integration & Functional
Grails 1.1 Testing - Unit, Integration & Functional
 
Deep Dive Modern Apps Lifecycle with Visual Studio 2012: How to create cross ...
Deep Dive Modern Apps Lifecycle with Visual Studio 2012: How to create cross ...Deep Dive Modern Apps Lifecycle with Visual Studio 2012: How to create cross ...
Deep Dive Modern Apps Lifecycle with Visual Studio 2012: How to create cross ...
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 
AngularJS Beginner Day One
AngularJS Beginner Day OneAngularJS Beginner Day One
AngularJS Beginner Day One
 
Codeception
CodeceptionCodeception
Codeception
 
Using Page Objects
Using Page ObjectsUsing Page Objects
Using Page Objects
 
Building Maintainable Android Apps (DroidCon NYC 2014)
Building Maintainable Android Apps (DroidCon NYC 2014)Building Maintainable Android Apps (DroidCon NYC 2014)
Building Maintainable Android Apps (DroidCon NYC 2014)
 
Groovy and Grails intro
Groovy and Grails introGroovy and Grails intro
Groovy and Grails intro
 

Mehr von Dmitry Buzdin

Developing Useful APIs
Developing Useful APIsDeveloping Useful APIs
Developing Useful APIs
Dmitry Buzdin
 
Архитектура Ленты на Одноклассниках
Архитектура Ленты на ОдноклассникахАрхитектура Ленты на Одноклассниках
Архитектура Ленты на Одноклассниках
Dmitry Buzdin
 
Riding Redis @ask.fm
Riding Redis @ask.fmRiding Redis @ask.fm
Riding Redis @ask.fm
Dmitry Buzdin
 
Rubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part IIRubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part II
Dmitry Buzdin
 
Rubylight Pattern-Matching Solutions
Rubylight Pattern-Matching SolutionsRubylight Pattern-Matching Solutions
Rubylight Pattern-Matching Solutions
Dmitry Buzdin
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
Dmitry Buzdin
 
Rubylight programming contest
Rubylight programming contestRubylight programming contest
Rubylight programming contest
Dmitry Buzdin
 
Continuous Delivery
Continuous Delivery Continuous Delivery
Continuous Delivery
Dmitry Buzdin
 
Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOps
Dmitry Buzdin
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
Dmitry Buzdin
 

Mehr von Dmitry Buzdin (20)

How Payment Cards Really Work?
How Payment Cards Really Work?How Payment Cards Really Work?
How Payment Cards Really Work?
 
Как построить свой фреймворк для автотестов?
Как построить свой фреймворк для автотестов?Как построить свой фреймворк для автотестов?
Как построить свой фреймворк для автотестов?
 
How to grow your own Microservice?
How to grow your own Microservice?How to grow your own Microservice?
How to grow your own Microservice?
 
Delivery Pipeline for Windows Machines
Delivery Pipeline for Windows MachinesDelivery Pipeline for Windows Machines
Delivery Pipeline for Windows Machines
 
Big Data Processing Using Hadoop Infrastructure
Big Data Processing Using Hadoop InfrastructureBig Data Processing Using Hadoop Infrastructure
Big Data Processing Using Hadoop Infrastructure
 
JOOQ and Flyway
JOOQ and FlywayJOOQ and Flyway
JOOQ and Flyway
 
Developing Useful APIs
Developing Useful APIsDeveloping Useful APIs
Developing Useful APIs
 
Whats New in Java 8
Whats New in Java 8Whats New in Java 8
Whats New in Java 8
 
Архитектура Ленты на Одноклассниках
Архитектура Ленты на ОдноклассникахАрхитектура Ленты на Одноклассниках
Архитектура Ленты на Одноклассниках
 
Dart Workshop
Dart WorkshopDart Workshop
Dart Workshop
 
Riding Redis @ask.fm
Riding Redis @ask.fmRiding Redis @ask.fm
Riding Redis @ask.fm
 
Rubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part IIRubylight JUG Contest Results Part II
Rubylight JUG Contest Results Part II
 
Rubylight Pattern-Matching Solutions
Rubylight Pattern-Matching SolutionsRubylight Pattern-Matching Solutions
Rubylight Pattern-Matching Solutions
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
 
Rubylight programming contest
Rubylight programming contestRubylight programming contest
Rubylight programming contest
 
Continuous Delivery
Continuous Delivery Continuous Delivery
Continuous Delivery
 
Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOps
 
Thread Dump Analysis
Thread Dump AnalysisThread Dump Analysis
Thread Dump Analysis
 
Mlocjs buzdin
Mlocjs buzdinMlocjs buzdin
Mlocjs buzdin
 

Pragmatic Java Test Automation