Saturday, 31 December 2011

How to Generating code in Eclipse for Selenium automation


7. Generating code

Eclipse has several possibilities to generate code for you. This can save significant time during development.
For example Eclipse can override methods from superclasses and generate the toString()hashcode() andequals() methods. It can also generate getter and setter methods for attributes of your Java class.
You can find these options in the Source menu.

Code generation

To test the source generation, create the following class in your de.vogella.eclipse.ide.first project.

   
package de.vogella.eclipse.ide.first;

public class Person {
private String firstName;
private String lastName;

}


Select Source → Generate Constructor from Fields, mark both fields and press "Ok".

Generating

Select Source → Generate Getter and Setter, select again both your fields and press the "Ok" button.
Select Source → Generate toString(), mark again both fields and press "Ok".
You created the following class:

   
package de.vogella.eclipse.ide.first;

public class Person {
private String firstName;
private String lastName;

public Person(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ "]";
}

}