inf1 op
play

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


  1. Inf1-OP Bonus Lecture - JUnit, Lambdas and Streams Volker Seeker School of Informatics March 20, 2019

  2. Automatic Testing with JUnit

  3. Test Driven Development Source: https://dzone.com/articles/what-is-refactoring

  4. Simple Calculator Calculator +add(int, int):int +mul(int, int):int +incrementAll(int[], int):void Implement a utility class with calculator functionality.

  5. How would you test the functionality of a class? Demo

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

  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:

  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

  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!

  10. Automatic evaluation with assertions Demo

  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!

  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

  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!

  14. Organising Tests with a Test Framework Demo

  15. Summary Automatic Testing with JUnit ◮ Tests results are evaluated automatically

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

  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

  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

  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

  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

  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

  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.

  23. ArrayLists and Hashtables Recap

  24. Lists Demo

  25. Maps Demo

  26. Lambdas

  27. Filter a list of numbers by a threshold Demo

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

  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.

  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!.

  31. Filter a list of numbers by a threshold with an Anonymous class Demo

  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.

  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!

  34. Filter a list of numbers by a threshold with a Lambda expression Demo

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

  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

  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

  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

  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

  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 options

  41. Streams

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

  43. Performing computations using Java streams. Demo

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

  45. Questions?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend