
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…
- Can switch expressions work with more than one assignment?
No, if you try to use a switch expression to assign a value to multiple variables, it will compile. However, if I try to access the value of id here, I get a compilation error saying it has not been initialized.
123456String id, customerId = switch(name){case "john" -> "12212";case "mary" -> "4847474";case "tom" -> "293743";default -> "";}; - 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.
1234567891011public record Account(int id,int customerId,String type,double balance){//COMPILATION ERRORpublic Account withId(int id){this.id = id;}} - Can I use overloading with records?
Yep, this is perfectly fine.
12345678910public record Account(int id, int customerId, String type, double balance){public void myMethod(){}public void myMethod(String value){}} - Can records inherit from other classes or records?
Since records are final, it doesn’t seem like this is possible.Given this class…
1public class AccountsMom {}…attempting to have a record extend a class results in a compilation error: No extends clause allowed for record
12345public record Account(int id,int customerId,String type,double balance) extends AccountsMom {}And if we change AccountsMom to a record…
1public record AccountsMom(){}… then attempt to have Account extend AccountsMom, we also get a compilation error: Cannot inherit from final ‘models.AccountsMom’
12345public record Account(int id,int customerId,String type,double balance) extends AccountsMom {} - 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.
1234public interface AccountsMom {void momsMethod();}12345678910public record Account(int id,int customerId,String type,double balance) implements AccountsMom{public void momsMethod(){}} - 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.
1Account expectedAccount = new Account(123, 456, "CHECKING", 100.00); - 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…
1234if (myCar instanceof Truck) {Truck myTruck = (Truck)myCar;//do something}You can do…
123if (myCar instanceof Truck myTruck) {//do something} - 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.