These days POJO's (Plain Old Java Objects) are used in all kinds of Java Projects.
A Typical POJO consists of fields, all related getters/setters, and sometimes equals() & hashCode() methods.
Sample POJO:
public class Product {
private Integer Id;
private String name;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
//equality logic
}
@Override
public int hashCode() {
//some logic
}
}
By using lombok jar, we can simulate getters/setters methods, equals() method, hashCode() method and many other features. To simulate these methods just you need to use their respective annotations. Every feature has its own annotations.
For Example: To simulate setter methods - just use @Setter on the required fields
@Setter
private Integer Id;
@Setter
private String name;
First you need to download the lombok.jar Lombok.jar
Different IDE's has different way of configuring lombok jar - If you would want to use in eclipse IDE - need to install first.
Usage on other IDEs can be found at Download link page.
Installing lombok on Eclipse:
>> open commond prompt - > go to lombok jar directory
(If not downloaded lombok.jar - do it first)
>> run the jar using the following command
<path to lombok jar>$ java -jar lombok.jar
>> This opens a lombok installer window
>> Now, Click the Specify location.. - select the eclipse IDE installation location.
>> In the above dialog, you must browse to Eclipse IDE installation directory and must choose eclipse.ini as labelled orange in the above pic.
>>Click Select
>> Given a correct installation directory - goes to the above screen. Click Install/Update.
You are done with installation of Lombok.
(If you observe IDE directory - a copy lombok.jar would be added to IDE installation directory.)
**Note:
Even though, you have installed lombok on eclipse IDE- you need lombok jar file on your classpath again.
So, In my example I using a lombok maven dependecny (so it automatically get my jar to the classpath)
Maven Dependency:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
</dependency>
Sample POJO with lombok annoations:
@Setter - To Simulate Setter Methods
@Getter - To Simulate Getter Methods
@ToString - toString Method
@EqualsAndHashCode - To Simulate equals and hashcode Methods.
This POJO is equivalent to the Sample POJO above.
We can test this POJO by writing a simple TestNG test:
Also add a TestNG jar to run your tests successfully:
More Features: lombok features
Please Share - Hope you like it.
A Typical POJO consists of fields, all related getters/setters, and sometimes equals() & hashCode() methods.
Sample POJO:
public class Product {
private Integer Id;
private String name;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object obj) {
//equality logic
}
@Override
public int hashCode() {
//some logic
}
}
By using lombok jar, we can simulate getters/setters methods, equals() method, hashCode() method and many other features. To simulate these methods just you need to use their respective annotations. Every feature has its own annotations.
For Example: To simulate setter methods - just use @Setter on the required fields
@Setter
private Integer Id;
@Setter
private String name;
First you need to download the lombok.jar Lombok.jar
Different IDE's has different way of configuring lombok jar - If you would want to use in eclipse IDE - need to install first.
Usage on other IDEs can be found at Download link page.
Installing lombok on Eclipse:
>> open commond prompt - > go to lombok jar directory
(If not downloaded lombok.jar - do it first)
>> run the jar using the following command
<path to lombok jar>$ java -jar lombok.jar
>> This opens a lombok installer window
>> Now, Click the Specify location.. - select the eclipse IDE installation location.
>> In the above dialog, you must browse to Eclipse IDE installation directory and must choose eclipse.ini as labelled orange in the above pic.
>>Click Select
>> Given a correct installation directory - goes to the above screen. Click Install/Update.
You are done with installation of Lombok.
(If you observe IDE directory - a copy lombok.jar would be added to IDE installation directory.)
**Note:
Even though, you have installed lombok on eclipse IDE- you need lombok jar file on your classpath again.
So, In my example I using a lombok maven dependecny (so it automatically get my jar to the classpath)
Maven Dependency:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
</dependency>
Sample POJO with lombok annoations:
package com.examples; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import lombok.ToString; @ToString @EqualsAndHashCode public class Product { @Setter @Getter private Integer Id; @Setter @Getter private String name; }
@Setter - To Simulate Setter Methods
@Getter - To Simulate Getter Methods
@ToString - toString Method
@EqualsAndHashCode - To Simulate equals and hashcode Methods.
This POJO is equivalent to the Sample POJO above.
We can test this POJO by writing a simple TestNG test:
Also add a TestNG jar to run your tests successfully:
package com.test; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.Assert; import com.examples.Product; public class LombokTest { Product product1 = new Product(); Product product2 = new Product(); @BeforeMethod public void setUp() { // accessing setters on POJO product1.setId(1); product1.setName("Washing Machine"); product2.setId(2); product2.setName("Gas Stove"); } @Test public void testProducts() { // accessing getters Assert.assertEquals("Washing Machine", product1.getName()); Assert.assertEquals("Gas Stove", product2.getName()); } @Test public void testObjectEquality() { // shows they are different objects Assert.assertNotEquals(product1, product2); } }Download Complete Example: Source Code
More Features: lombok features
Please Share - Hope you like it.