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 +add(int, int):int +mul(int, int):int +incrementAll(int[], int):void Implement a utility class with calculator functionality.
How would you test the functionality of a class? Demo
Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code.
Main Method as Test Client Main methods can be used to quickly evaluate the functionality of your code. They have, however, a few drawbacks:
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
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!
Automatic evaluation with assertions Demo
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!
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
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!
Organising Tests with a Test Framework Demo
Summary Automatic Testing with JUnit ◮ Tests results are evaluated automatically
Summary Automatic Testing with JUnit ◮ Tests results are evaluated automatically ◮ Tests are organised in a standard way
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
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
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
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
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
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.
ArrayLists and Hashtables Recap
Lists Demo
Maps Demo
Lambdas
Filter a list of numbers by a threshold Demo
Filtering a List The Predicate interface specifies an API which classes can implement to provide filter functionality.
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.
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!.
Filter a list of numbers by a threshold with an Anonymous class Demo
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.
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!
Filter a list of numbers by a threshold with a Lambda expression Demo
Summary Lambda expressions in Java ◮ A lambda expression is an anonymous function
Summary Lambda expressions in Java ◮ A lambda expression is an anonymous function ◮ It can be called without an explicit identifier, i.e. method name
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
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
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
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 options
Streams
Streams A stream ◮ represents a sequence of elements ◮ supports different kinds of operations This is where lambda expressions become very handy.
Performing computations using Java streams. Demo
Extensive Stream Tutorial: https://winterbe.com/posts/2014/07/31/ java8-stream-tutorial-examples/
Questions?
Recommend
More recommend