Top

Test Automation Beyond Java 8

java 9 - 14

Test Automation Beyond Java 8

While most people are still using Java 8 from six years ago, new versions of the language are being released every six months!

The latest Java versions provide an abundance of new features, such as functional programming capabilities, switch expressions, local variable type inference, additions to the Stream API, new factory methods for Collections, text blocks and much more.

With so much content, your Java upgrade should be more than simply the update of a jar file. Your test code no longer has to be as verbose as it once was — this means less code to write and maintain!

This talk introduces some of the new features of Java 9 – 14 and provides examples of how these features can be used for test automation.

Key Takeaways:

  • Introduction of new features in the latest 6 releases of Java (Java 9 – 14)
  • Examples of how the features can be used for test automation projects
  • Greater command of the programming language

 

Video

 

 

Slides

 

 

Q&A

 

There were some excellent questions during the Q&A of this talk. I hadn’t tried some of these things before so I couldn’t provide more than just an educated guess. But after the talk, I took the time to play around with the features to be able to definitively answer your questions, so here goes…

  1. Can switch expressions work with more than one assignment?

     

    Yes, you can use a switch expression to assign a value to multiple variables.

     

  2. Is it possible to define builders inside of records?

     

    Technically, you can add methods inside of a record but all the fields are final, so I can’t think of a scenario where having a Builder inside of a record would make much sense. When I attempted to do this, I got a compilation error: Cannot assign a value to final variable.

     

  3. Can I use overloading with records?

     

    Yep, this is perfectly fine.

     

  4. Can records inherit from other classes or records?

     

    Since records are final, it doesn’t seem like this is possible.Given this class…

    …attempting to have a record extend a class results in a compilation error: No extends clause allowed for record

     

    And if we change AccountsMom to a record…

    … then attempt to have Account extend AccountsMom, we also get a compilation error: Cannot inherit from final ‘models.AccountsMom’

     

  5. Can records implement interfaces?

     

    Based on what we learned so far, I thought the answer would be no, but surprise surprise, this can be done! So, yes, records can implement interfaces.

     

  6. Can you do serialization with records?
     
    Yes, you can instantiate it just like you would for any object and then use that object as payload for an API request.

     

  7. Was typecasting made easier using newer versions?

     

    Yes! Java 14 introduced instanceof pattern matching which saves you a step. As opposed to first checking for type and then casting, you can now do it all in one line. So instead of…

    You can do…

     

  8. Is there a new feature for bulk API methods?
     
    I’ve not seen native enhancements to enable bulk API, but there have been some cool improvements to HTTPClient in general in Java 9 and 11.

 

See Code on Github

Angie Jones
No Comments

Post a Comment