Notice

╠ This is my personal blog and my posts here have nothing to do with my employers or any other association I may have. It is my personal blog for my personal experience, ideas and notes. ╣
Showing posts with label Java Beans. Show all posts
Showing posts with label Java Beans. Show all posts

Sunday, August 9, 2020

New way to create immutable object in Java ['record'] JEP 384: Records

As a Java developer, we use POJO every day in our coding. We have to write or generate codes of accessor methods and other methods like 'equals', 'toString' and 'hashCode' or use a library like Lombok. 

To make it more simplified Java introducing 'record'. Using which developer need not have to generate or write these code. 

Some of the features of these 'record' class. 

  • implicitly they are final so it cannot be extended or abstracted but can implement an interface. 
  • java.lang.Record is the superclass of record 
  • A record cannot define the native method. 
  • A record field(s)  is/are implicitly final
Let's see with an example. 

A simple record with no boilerplate code. It provides field accessor method, 'toString', 'equals' and 'hashCode' readily available. 

A record with multiple constructors and instance method. 

A record with constructor level validation, static method and override 'equals' method. 

In this class, we are accessing the field with the internal method and other methods like 'toString', 'equals' and 'hashCode'. 


Output 

To compile these codes 

javac --enable-preview --release 15 TestRecord.java


To Run these codes 

java --enable-preview TestRecord



Happy coding and keep learning!

Friday, January 26, 2018

Java Unit Testing of POJO class

I have created a tool to help the developer to do test coverage of their POJO Bean classes.

Java POJO classes are supposed to be tested while testing any business logic but in the real world, it does not happen. So I created this framework which will help developers while showing coverage to reviewers & auditors, and also help to discover silly mistakes which happen mostly during field name refactoring.

 I personally recommend developers that try to get these POJO tested while testing business logic if you somehow could not do it then this tool comes second.

This is a very simple to use the framework with lots of features which developers required in the real world.





Here is Wiki to learn this framework.


I am open to your feedback, please try it and let me know your feedback.