I have a class Address
, a class AddressTest
and I am trying to write unit tests for its constructors and getter setters.
Address:
/**
* The bean Address.
*
* @author Sandeep Chatterjee
* @since 24/8/2015
*/
class Address {
private BigInteger addressId;
private String name;
private String address;
private String contactNo;
private String postalCode;
/**
* Constructor
*/
public Address() {
}
/**
* Constructor
*
* @param name Addressee Name
* @param address Full Postal Address
* @param contactNo Addressee Contact Number
* @param postalCode Addressee postal code
*/
private Address(String name, String address, String contactNo, String postalCode) {
this.addressId = new BigInteger(256, new Random());
this.name = name;
this.address = address;
this.contactNo = contactNo;
this.postalCode = postalCode;
}
/**
* Static factory
*
* @param name Addressee Name
* @param address Full Postal Address
* @param contactNo Addressee Contact Number
* @param postalCode Addressee postal code
* @return An Address instance
*/
public static Address createAddress(String name, String address, String contactNo, String postalCode) {
return new Address(name, address, contactNo, postalCode);
}
/* Object overrides */
@Override
public String toString() {
return new ToStringBuilder(this)
.append("address", address)
.toString();
}
@Override
public final int hashCode() {
return Objects.hashCode(addressId);
}
@Override
public final boolean equals(Object object) {
if (object instanceof Address) {
Address that = (Address) object;
return Objects.equal(this.addressId, that.addressId);
}
return false;
}
/* Accessors and Mutators */
/**
* @return Get Address Id
*/
public BigInteger getAddressId() {
return addressId;
}
/**
* @param addressId Set Address Id
*/
public void setAddressId(BigInteger addressId) {
this.addressId = addressId;
}
/**
* @return Get Addressee Name
*/
public String getName() {
return name;
}
/**
* @param name Set Addressee Name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Get Full Postal Address
*/
public String getAddress() {
return address;
}
/**
* @param address Set Full Postal Address
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return Get Addressee Contact Number
*/
public String getContactNo() {
return contactNo;
}
/**
* @param contactNo Set Addressee Contact Number
*/
public void setContactNo(String contactNo) {
this.contactNo = contactNo;
}
/**
* @return Get Addressee postal code
*/
public String getPostalCode() {
return postalCode;
}
/**
* @param postalCode Set Addressee postal code
*/
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
}
AddressTest:
/**
* All tests for class Address
*
* @author Sandeep Chatterjee
* @since 24/8/2015
*/
public class AddressTest {
/**
* @see [http://www.jqno.nl/equalsverifier/]
* @see [https://github.com/jqno/equalsverifier/blob/master/README.md]
*/
@Test
public void equalsContract() {
EqualsVerifier.forClass(Address.class)
.suppress(Warning.NONFINAL_FIELDS, Warning.NULL_FIELDS)
.verify();
}
@Test
public void testPrivateConstructor() {
final Constructor<?>[] constructors = Address.class.getDeclaredConstructors();
for (Constructor<?> constructor : constructors) {
Assert.assertTrue(Modifier.isPrivate(constructor.getModifiers()));
}
}
@Test
public void testGetterSetterPostalCode() {
final String postalCode = "456098";
final Address address = new Address();
address.setPostalCode("456098");
Assert.assertEquals(address.getPostalCode(), postalCode);
}
}
Am I doing this correctly or is there a better way? Any help appreciated.