username:rajeshbhojwani "Java toString() method" (#6047)

* Check in code for Java toString method

* fix formatting issues

* Fix formatting issues

* Fix formatting issues

* Fix formatting issues

* Fix formatting issues

* Fix formatting issues

* replacing orders with order

* Update formatting

* Fix formatting

* Fix formatting

* Fix formatting

* Fix formatting

* Fix formatting
This commit is contained in:
Rajesh Bhojwani
2019-01-07 14:01:13 +05:30
committed by Eugen
parent 6f1e14b649
commit 65b9909fed
12 changed files with 340 additions and 0 deletions
@@ -0,0 +1,26 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomerArrayToStringTest {
private static final String CUSTOMER_ARRAY_TO_STRING
= "Customer [orders=[Order [orderId=A1111, desc=Game, value=0]], getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenArray_whenToString_thenCustomerDetails() {
CustomerArrayToString customer = new CustomerArrayToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
Order[] orders = new Order[1];
orders[0] = new Order();
orders[0].setOrderId("A1111");
orders[0].setDesc("Game");
orders[0].setStatus("In-Shiping");
customer.setOrders(orders);
assertEquals(CUSTOMER_ARRAY_TO_STRING, customer.toString());
}
}
@@ -0,0 +1,25 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomerComplexObjectToStringTest {
private static final String CUSTOMER_COMPLEX_TO_STRING
= "Customer [order=Order [orderId=A1111, desc=Game, value=0], getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenComplex_whenToString_thenCustomerDetails() {
CustomerComplexObjectToString customer = new CustomerComplexObjectToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
Order order = new Order();
order.setOrderId("A1111");
order.setDesc("Game");
order.setStatus("In-Shiping");
customer.setOrder(order);
assertEquals(CUSTOMER_COMPLEX_TO_STRING, customer.toString());
}
}
@@ -0,0 +1,22 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomerPrimitiveToStringTest {
private static final String CUSTOMER_PRIMITIVE_TO_STRING
= "Customer [balance=110, getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenPrimitive_whenToString_thenCustomerDetails() {
CustomerPrimitiveToString customer = new CustomerPrimitiveToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
customer.setBalance(110);
assertEquals(CUSTOMER_PRIMITIVE_TO_STRING, customer.toString());
}
}
@@ -0,0 +1,32 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class CustomerReflectionToStringTest {
private static final String CUSTOMER_REFLECTION_TO_STRING = "com.baeldung.string.tostring.CustomerReflectionToString";
@Test
public void givenWrapperCollectionStrBuffer_whenReflectionToString_thenCustomerDetails() {
CustomerReflectionToString customer = new CustomerReflectionToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
customer.setScore(8);
List<String> orders = new ArrayList<String>();
orders.add("Book");
orders.add("Pen");
customer.setOrders(orders);
StringBuffer fullname = new StringBuffer();
fullname.append(customer.getLastName()+", "+ customer.getFirstName());
customer.setFullname(fullname);
assertTrue(customer.toString().contains(CUSTOMER_REFLECTION_TO_STRING));
}
}
@@ -0,0 +1,33 @@
package com.baeldung.string.tostring;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
public class CustomerWrapperCollectionToStringTest {
private static final String CUSTOMER_WRAPPER_COLLECTION_TO_STRING
= "Customer [score=8, orders=[Book, Pen], fullname=Bhojwani, Rajesh, getFirstName()=Rajesh, getLastName()=Bhojwani]";
@Test
public void givenWrapperCollectionStrBuffer_whenToString_thenCustomerDetails() {
CustomerWrapperCollectionToString customer = new CustomerWrapperCollectionToString();
customer.setFirstName("Rajesh");
customer.setLastName("Bhojwani");
customer.setScore(8);
List<String> orders = new ArrayList<String>();
orders.add("Book");
orders.add("Pen");
customer.setOrders(orders);
StringBuffer fullname = new StringBuffer();
fullname.append(customer.getLastName()+", "+ customer.getFirstName());
customer.setFullname(fullname);
assertEquals(CUSTOMER_WRAPPER_COLLECTION_TO_STRING, customer.toString());
}
}