pragmatic java test automation

52
Java Test Automation Real-world patterns and practices Dmitry Buzdin

Upload: neueda

Post on 10-May-2015

4.534 views

Category:

Documents


6 download

TRANSCRIPT

Page 1: Pragmatic Java Test Automation

Java Test AutomationReal-world patterns and practices

Dmitry Buzdin

Page 2: Pragmatic Java Test Automation

TESTING AUTOMATION

Page 3: Pragmatic Java Test 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

Page 4: Pragmatic Java Test Automation

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

Page 5: Pragmatic Java Test Automation

Testing patterns in this presentation are applicable to any framework/system

Page 6: Pragmatic Java Test Automation

SAMPLE APPLICATION

Page 7: Pragmatic Java Test Automation

User Interface

Application Services

Domain Model

Integration Components

External Services & Storage

Typical Layers

Page 8: Pragmatic Java Test Automation

Our Requirements

Display weather forecasts

Store them in database

Lots of other usual requirements

Cloud, HTML5, BigData...

Page 9: Pragmatic Java Test Automation

Weather REST APISQL DB

Integration Layer

Other APIs

ORM

Domain Model

Service Layer

REST API

Web Client

Page 10: Pragmatic Java Test Automation

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

Page 12: Pragmatic Java Test Automation

DEMO

Page 13: Pragmatic Java Test Automation

TESTING STRATEGY

Page 14: Pragmatic Java Test Automation

Unit Tests

Integration Tests

Functional Tests

Page 15: Pragmatic Java Test Automation

UNIT TESTING

Page 16: Pragmatic Java Test Automation

Unit Tests

Classes are tested in isolation (almost)

One test method checks one use-case

One class <-> one test class

Page 17: Pragmatic Java Test Automation

Weather REST APISQL DB

Integration Layer

Other APIs

ORM

Domain Model

Service Layer

REST API

Web Client

Page 18: Pragmatic Java Test Automation

JUnit Tests

http://junit.org/

@Beforepublic void setUp() { // Preparing object for test}

@Testpublic void shouldFindWeatherByCity() throws Exception {

// Conditions set-up// Method under test invocation// Assertions

}

Page 19: Pragmatic Java Test Automation

Use Dependency Injection

Separation of classes

Mostly about testing

Popular DI frameworks

Spring

CDI

Guice

Page 20: Pragmatic Java Test Automation

Injection Types

by constructor

by setter

by field

Page 21: Pragmatic Java Test Automation

Mockito Mocks

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

// Mocking all class dependenciesservice = new WeatherServiceImpl();service.weatherSource = Mockito.mock(WeatherSource.class);

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

// Your method under test here

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

Page 22: Pragmatic Java Test Automation

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/

Page 23: Pragmatic Java Test Automation

Benefits

Forget assertEquals() !

Hamcrest matchers are

expressive

flexible

extendable

Page 24: Pragmatic Java Test Automation

DEMO

Page 25: Pragmatic Java Test Automation

INTEGRATION TESTING

Page 26: Pragmatic Java Test Automation

Integration Tests

Should not start the whole application

Testing integration components

Remote API calls

Data conversion

Fault-scenarios

Page 27: Pragmatic Java Test Automation

Weather REST APISQL DB

Integration Layer

Other APIs

ORM

Domain Model

Service Layer

REST API

Web Client

Page 28: Pragmatic Java Test Automation

Fake Dependencies

Write code for emulating dependencies

Fake Web Service

Fake FTP server

Fake InputStream

Fake XML response

Page 29: Pragmatic Java Test Automation

DEMO

Page 30: Pragmatic Java Test Automation

Persistence Tests

Isolate and test all persistence operations

Ideally all CRUD operations

Could be done in Generic way

Page 31: Pragmatic Java Test Automation

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/

Page 32: Pragmatic Java Test Automation

DEMO

Page 33: Pragmatic Java Test Automation

FUNCTIONAL TESTING

Page 34: Pragmatic Java Test Automation

Functional Tests

Not testing UI

Starting application context

Emulating multiple user requests

Persisting intermediate results

Page 35: Pragmatic Java Test Automation

Weather REST APISQL DB

Integration Layer

Other APIs

ORM

Domain Model

Service Layer

REST API

Web Client

Page 36: Pragmatic Java Test Automation

Embedded Container

Test should start application

It is possible to start embedded

CDI Container

EJB Container

Spring Container

Page 37: Pragmatic Java Test Automation

Functional Testing using Service Layer

Ignoring GUI data conversion

Easier to test

Faster test execution

Page 38: Pragmatic Java Test Automation

Mock Integrations

Could be replaced using

Properties and factory beans

Spring @Profile

CDI @Alternative

Guice Modules

Page 39: Pragmatic Java Test Automation

Environment Switch

Application should run in several modes

Done with System environment variable

Typical modes:

production

local deployment

embedded testing

Page 40: Pragmatic Java Test Automation

Configuration Override

Environments overrides settings

Hierarchical configurations

Properties, YAML or other

Page 41: Pragmatic Java Test Automation

Initial Data

Reset database after every test and insert initial data

Reuse ORM mapping or other persistence layer

Or use specialized tools like DbUnit

Page 42: Pragmatic Java Test Automation

API-Level Testing

Write test for your API

Some examples:

SOAP

REST

EJB

Page 43: Pragmatic Java Test Automation

DEMO

Page 44: Pragmatic Java Test Automation

Weather REST APISQL DB

Integration Layer

Other APIs

ORM

Domain Model

Service Layer

REST API

Web Client

Page 45: Pragmatic Java Test Automation

Embedded Web Server

It is possible to run embedded

Jetty

Glassfish/Grizzly

Page 46: Pragmatic Java Test Automation

JUnit Rules

@Rulepublic EmbeddedJetty jetty = new EmbeddedJetty();

@Testpublic void shouldTestEmbeddedJetty() { ...}

Page 47: Pragmatic Java Test Automation

DEMO

Page 48: Pragmatic Java Test Automation

SUMMARY

Page 49: Pragmatic Java Test Automation

Why is it all needed?

Decrease cost of change

Increase software quality

Remove fear of making changes

Page 50: Pragmatic Java Test Automation

When Testing is Enough?

If CI build passes it is safe to deploy to

production

Page 51: Pragmatic Java Test Automation

What was not covered?

Automated

Acceptance testing

GUI-level testing

Performance testing