java beginners meetup february 2017: testing and tdd

31
An introduction to testing and TDD Patrick Kostjens February 7, 2017 Java Beginners Meetup

Upload: patrick-kostjens

Post on 21-Feb-2017

67 views

Category:

Technology


2 download

TRANSCRIPT

An introduction to testing and TDD

Patrick Kostjens

February 7, 2017

Java Beginners Meetup

Table of contents

1. Introduction

2. Test Driven Development

3. Unit testing in Java

4. Exercise 1

5. Exercise 2

1

Introduction

About me

Patrick Kostjens

Software Developer

2

Why testing?

To build better software faster

3

Better software

• Fewer bugs

• Easier to maintain

• Easier to change

• Safer to change

4

How to test?

Write code that checks if other code does what it issupposed to.

5

Types of testing

1. Unit testing

2. Integration testing

3. System testing

4. Acceptance testing

6

Testing concepts

• Mocking

• e2e

• TDD

• BDD

7

Test Driven Development

Brief introduction

Writing tests to check if your code works seems good, right?

But, maybe we are only testing what we expect to happen.

What if we write our tests before we write (bits of) code?

Test Driven Development

8

Brief introduction

Writing tests to check if your code works seems good, right?

But, maybe we are only testing what we expect to happen.

What if we write our tests before we write (bits of) code?

Test Driven Development

8

Brief introduction

Writing tests to check if your code works seems good, right?

But, maybe we are only testing what we expect to happen.

What if we write our tests before we write (bits of) code?

Test Driven Development

8

Brief introduction

Writing tests to check if your code works seems good, right?

But, maybe we are only testing what we expect to happen.

What if we write our tests before we write (bits of) code?

Test Driven Development

8

TDD workflow

9

Why TDD?

• No influence from knowledge gained while implementing

• Immediately know if every bit of code works

• Keep code as simple as possible

• Make code easy to test

• Catch bugs early

10

Why TDD?

• No influence from knowledge gained while implementing

• Immediately know if every bit of code works

• Keep code as simple as possible

• Make code easy to test

• Catch bugs early

10

Why TDD?

• No influence from knowledge gained while implementing

• Immediately know if every bit of code works

• Keep code as simple as possible

• Make code easy to test

• Catch bugs early

10

Why TDD?

• No influence from knowledge gained while implementing

• Immediately know if every bit of code works

• Keep code as simple as possible

• Make code easy to test

• Catch bugs early

10

Why TDD?

• No influence from knowledge gained while implementing

• Immediately know if every bit of code works

• Keep code as simple as possible

• Make code easy to test

• Catch bugs early

10

Unit testing in Java

Tools and frameworks

Frameworks:

• JUnit

• TestNG

Tools:

• Mockito

• EasyMock

• Powermock

• Hamcrest

11

Simple test in JUnit

import org.junit.Test;

import org.junit.Assert;

public class SimpleTest {

@Test

public void test1Returns1 () {

Assert.assertEquals("1",

getAnswer (1));

}

}

12

Test preparation

public class PrepareTest {

private FizzBuzz testObject;

@Before

public void setup() {

testObject = new FizzBuzz ();

}

@Test

//...

}

13

Exercise 1

Fizz Buzz project

Take repository at:

https://github.com/patrickkostjens/fizz-buzz-exercise

Install and run using README

14

Fizz Buzz rules

• Dividable by 3? Fizz

• Dividable by 5? Buzz

• Dividable by 3 and 5? Fizz Buzz

• None of the above? Just a number

Exercise

Given a number, return the Fizz Buzz answer for thatnumber.

15

Easy, right?

Fizz Buzz seems to be very easy

yet

there are many solutions

See: https://ditam.github.io/posts/fizzbuzz/

16

Exercise 2

Bowling scoring system

• No project setup available

• Don’t build an interface

• Start simple!

• Try to do TDD

17

Thank you!

17