[BAEL-5245] Lombok ToString annotation (#11870)
This commit is contained in:
parent
2eb49b96cb
commit
7c3cbf0d4f
|
@ -0,0 +1,56 @@
|
|||
package com.baeldung.lombok.tostring;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
public class Account {
|
||||
|
||||
private String name;
|
||||
|
||||
// render this field before any others (the highest ranked)
|
||||
@ToString.Include(rank = 1)
|
||||
private String id;
|
||||
|
||||
@ToString.Exclude
|
||||
private String accountNumber;
|
||||
|
||||
// automatically excluded
|
||||
private String $ignored;
|
||||
|
||||
@ToString.Include
|
||||
String description() {
|
||||
return "Account description";
|
||||
}
|
||||
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
public void setAccountNumber(String accountNumber) {
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String get$ignored() {
|
||||
return $ignored;
|
||||
}
|
||||
|
||||
public void set$ignored(String value) {
|
||||
this.$ignored = value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.lombok.tostring;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
public enum AccountType {
|
||||
CHECKING,
|
||||
SAVING
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.lombok.tostring;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString
|
||||
public class RewardAccount extends Account {
|
||||
|
||||
private String rewardAccountId;
|
||||
|
||||
private Object[] relatedAccounts;
|
||||
|
||||
public String getRewardAccountId() {
|
||||
return rewardAccountId;
|
||||
}
|
||||
|
||||
public void setRewardAccountId(String rewardAccountId) {
|
||||
this.rewardAccountId = rewardAccountId;
|
||||
}
|
||||
|
||||
public Object[] getRelatedAccounts() {
|
||||
return relatedAccounts;
|
||||
}
|
||||
|
||||
public void setRelatedAccounts(Object[] relatedAccounts) {
|
||||
this.relatedAccounts = relatedAccounts;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.baeldung.lombok.tostring;
|
||||
|
||||
import lombok.ToString;
|
||||
|
||||
@ToString(callSuper = true)
|
||||
public class SavingAccount extends Account {
|
||||
|
||||
private String savingAccountId;
|
||||
|
||||
public String getSavingAccountId() {
|
||||
return savingAccountId;
|
||||
}
|
||||
|
||||
public void setSavingAccountId(String savingAccountId) {
|
||||
this.savingAccountId = savingAccountId;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.baeldung.lombok.tostring;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ToStringUnitTest {
|
||||
|
||||
@Test
|
||||
void whenPrintObject_thenOutputIsCorrect() {
|
||||
Account account = new Account();
|
||||
account.setId("12345");
|
||||
account.setName("An account");
|
||||
account.setAccountNumber("11111"); // should not be present in output
|
||||
account.set$ignored("ignored value"); // should not be present in output
|
||||
|
||||
assertThat(account.toString())
|
||||
.isEqualTo("Account(id=12345, name=An account, description=Account description)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPrintSubclassWithSuper_thenOutputIsCorrect() {
|
||||
SavingAccount savingAccount = new SavingAccount();
|
||||
savingAccount.setSavingAccountId("5678");
|
||||
savingAccount.setId("12345");
|
||||
savingAccount.setName("An account");
|
||||
|
||||
assertThat(savingAccount.toString())
|
||||
.isEqualTo("SavingAccount(super=Account(id=12345, name=An account, description=Account description), savingAccountId=5678)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPrintArrays_thenOutputIsCorrect() {
|
||||
RewardAccount account = new RewardAccount();
|
||||
account.setRewardAccountId("12345");
|
||||
|
||||
// circular ref, just for demonstration
|
||||
Object[] relatedAccounts = new Object[2];
|
||||
relatedAccounts[0] = "54321";
|
||||
relatedAccounts[1] = relatedAccounts;
|
||||
|
||||
account.setRelatedAccounts(relatedAccounts);
|
||||
|
||||
assertThat(account.toString())
|
||||
.isEqualTo("RewardAccount(rewardAccountId=12345, relatedAccounts=[54321, [...]])");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPrintSubclassWithoutSuper_thenOutputIsCorrect() {
|
||||
RewardAccount rewardAccount = new RewardAccount();
|
||||
rewardAccount.setRewardAccountId("12345");
|
||||
|
||||
assertThat(rewardAccount.toString())
|
||||
.isEqualTo("RewardAccount(rewardAccountId=12345, relatedAccounts=null)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenPrintEnum_thenOutputIsCorrect() {
|
||||
assertThat(AccountType.CHECKING.toString())
|
||||
.isEqualTo("AccountType.CHECKING");
|
||||
|
||||
assertThat(AccountType.SAVING.toString())
|
||||
.isEqualTo("AccountType.SAVING");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue