patternjavaMinor
Unit testing private constructors and getter setters
Viewed 0 times
constructorsgetterprivatetestingandsettersunit
Problem
I have a class
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 /
/**
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 /
/**
Solution
/**
* Constructor
*/
public Address() {
}You have just defeated your purpose of making a
private constructor... and not only that, it makes it possible to create an Address instance with null fields. Even if this should be possible in your codebase (as used in testGetterSetterPostalCode()), then I will suggest simply sticking to two public constructors - one with the arguments, and one without. The public static createAddress() then becomes unnecessary.I am also unsure about the viability of
testPrivateConstructor()... unit testing should be asserting the behavior of your class's features, not the visibility of its constructors and methods.Code Snippets
/**
* Constructor
*/
public Address() {
}Context
StackExchange Code Review Q#101772, answer score: 8
Revisions (0)
No revisions yet.