Inf1-OP Bonus Lecture - JUnit, Lambdas and Streams Volker Seeker - - PowerPoint PPT Presentation

inf1 op
SMART_READER_LITE
LIVE PREVIEW

Inf1-OP Bonus Lecture - JUnit, Lambdas and Streams Volker Seeker - - PowerPoint PPT Presentation

Inf1-OP Bonus Lecture - JUnit, Lambdas and Streams Volker Seeker School of Informatics March 20, 2019 Automatic Testing with JUnit Test Driven Development Source: https://dzone.com/articles/what-is-refactoring Simple Calculator Calculator


slide-1
SLIDE 1

Inf1-OP

Bonus Lecture - JUnit, Lambdas and Streams Volker Seeker

School of Informatics

March 20, 2019

slide-2
SLIDE 2

Automatic Testing with JUnit

slide-3
SLIDE 3

Test Driven Development

Source: https://dzone.com/articles/what-is-refactoring

slide-4
SLIDE 4

Simple Calculator

Calculator

+mul(int, int):int +add(int, int):int +incrementAll(int[], int):void

Implement a utility class with calculator functionality.

slide-5
SLIDE 5

How would you test the functionality of a class?

Demo

slide-6
SLIDE 6

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

slide-7
SLIDE 7

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

They have, however, a few drawbacks:

slide-8
SLIDE 8

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

They have, however, a few drawbacks: ◮ Using console output to evaluate test results requires manual effort and is error prone for more complex tests

slide-9
SLIDE 9

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

They have, however, a few drawbacks: ◮ Using console output to evaluate test results requires manual effort and is error prone for more complex tests → use assertions instead!

slide-10
SLIDE 10

Automatic evaluation with assertions

Demo

slide-11
SLIDE 11

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

They have, however, a few drawbacks: ◮ Using console output to evaluate test results requires manual effort and is error prone for more complex tests → use assertions instead!

slide-12
SLIDE 12

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

They have, however, a few drawbacks: ◮ Using console output to evaluate test results requires manual effort and is error prone for more complex tests → use assertions instead! ◮ tests are unorganised, no easy way to test only certain methods

slide-13
SLIDE 13

Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.

They have, however, a few drawbacks: ◮ Using console output to evaluate test results requires manual effort and is error prone for more complex tests → use assertions instead! ◮ tests are unorganised, no easy way to test only certain methods → use a test framework instead!

slide-14
SLIDE 14

Organising Tests with a Test Framework

Demo

slide-15
SLIDE 15

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically

slide-16
SLIDE 16

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way

slide-17
SLIDE 17

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way ◮ Tests can be executed in isolation for only specific features

slide-18
SLIDE 18

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way ◮ Tests can be executed in isolation for only specific features ◮ Tests can easily be excluded from shipped product

slide-19
SLIDE 19

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way ◮ Tests can be executed in isolation for only specific features ◮ Tests can easily be excluded from shipped product ◮ Framework functionality provides advanced capabilities

slide-20
SLIDE 20

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way ◮ Tests can be executed in isolation for only specific features ◮ Tests can easily be excluded from shipped product ◮ Framework functionality provides advanced capabilities

◮ @Before or @After methods are executed before and after each test to setup and clean up the test environment

slide-21
SLIDE 21

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way ◮ Tests can be executed in isolation for only specific features ◮ Tests can easily be excluded from shipped product ◮ Framework functionality provides advanced capabilities

◮ @Before or @After methods are executed before and after each test to setup and clean up the test environment ◮ @Test(timeout = 1000) allows testing for execution duration

slide-22
SLIDE 22

Summary

Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way ◮ Tests can be executed in isolation for only specific features ◮ Tests can easily be excluded from shipped product ◮ Framework functionality provides advanced capabilities

◮ @Before or @After methods are executed before and after each test to setup and clean up the test environment ◮ @Test(timeout = 1000) allows testing for execution duration ◮ etc.

slide-23
SLIDE 23

ArrayLists and Hashtables

Recap

slide-24
SLIDE 24

Lists

Demo

slide-25
SLIDE 25

Maps

Demo

slide-26
SLIDE 26

Lambdas

slide-27
SLIDE 27

Filter a list of numbers by a threshold

Demo

slide-28
SLIDE 28

Filtering a List The Predicate interface specifies an API which classes can implement to provide filter functionality.

slide-29
SLIDE 29

Filtering a List The Predicate interface specifies an API which classes can implement to provide filter functionality. However, writing a new class for each new filter is inconvenient.

slide-30
SLIDE 30

Filtering a List The Predicate interface specifies an API which classes can implement to provide filter functionality. However, writing a new class for each new filter is inconvenient. Let’s make this easier by using an Anonymous class!.

slide-31
SLIDE 31

Filter a list of numbers by a threshold with an Anonymous class

Demo

slide-32
SLIDE 32

Filtering a List The Predicate interface specifies an API which classes can implement to provide filter functionality. However, writing a new class for each new filter is inconvenient. Let’s make this easier by using an Anonymous class!. Anonymous classes help, but we can do better.

slide-33
SLIDE 33

Filtering a List The Predicate interface specifies an API which classes can implement to provide filter functionality. However, writing a new class for each new filter is inconvenient. Let’s make this easier by using an Anonymous class!. Anonymous classes help, but we can do better. Let’s use an anonymous function, i.e. a Lambda expression!

slide-34
SLIDE 34

Filter a list of numbers by a threshold with a Lambda expression

Demo

slide-35
SLIDE 35

Summary

Lambda expressions in Java ◮ A lambda expression is an anonymous function

slide-36
SLIDE 36

Summary

Lambda expressions in Java ◮ A lambda expression is an anonymous function ◮ It can be called without an explicit identifier, i.e. method name

slide-37
SLIDE 37

Summary

Lambda expressions in Java ◮ A lambda expression is an anonymous function ◮ It can be called without an explicit identifier, i.e. method name ◮ This makes it easy to pass them around as method arguments

slide-38
SLIDE 38

Summary

Lambda expressions in Java ◮ A lambda expression is an anonymous function ◮ It can be called without an explicit identifier, i.e. method name ◮ This makes it easy to pass them around as method arguments ◮ They can be used in place of any Functional Interface such as Predicate

slide-39
SLIDE 39

Summary

Lambda expressions in Java ◮ A lambda expression is an anonymous function ◮ It can be called without an explicit identifier, i.e. method name ◮ This makes it easy to pass them around as method arguments ◮ They can be used in place of any Functional Interface such as Predicate

◮ An interface with only a single abstract method

slide-40
SLIDE 40

Summary

Lambda expressions in Java ◮ A lambda expression is an anonymous function ◮ It can be called without an explicit identifier, i.e. method name ◮ This makes it easy to pass them around as method arguments ◮ They can be used in place of any Functional Interface such as Predicate

◮ An interface with only a single abstract method ◮ see java.util.function package for a large collection of available

  • ptions
slide-41
SLIDE 41

Streams

slide-42
SLIDE 42

Streams A stream ◮ represents a sequence of elements ◮ supports different kinds of operations This is where lambda expressions become very handy.

slide-43
SLIDE 43

Performing computations using Java streams.

Demo

slide-44
SLIDE 44

Extensive Stream Tutorial: https://winterbe.com/posts/2014/07/31/ java8-stream-tutorial-examples/

slide-45
SLIDE 45

Questions?