From ec0dacb31eca658dc33e6b9ec7a99ff24f07114e Mon Sep 17 00:00:00 2001 From: "arpit.khandelwal.1984" Date: Mon, 2 Jul 2018 12:56:01 +0530 Subject: [PATCH] Added integration test cases, updated unit test cases --- readme.md | 9 +- .../com/bankofspring/dto/CustomerDto.java | 2 +- .../com/bankofspring/exception/ApiError.java | 6 + .../service/customer/CustomerServiceImpl.java | 4 +- src/main/resources/images/logo.png | Bin 0 -> 9677 bytes .../BankOfSpringApplicationTests.java | 4 +- .../BankOfSpringApplicationUnitTests.java | 17 ++ .../{test => integrationtest}/.gitkeep | 0 .../controller/AccountControllerTest.java | 30 +-- .../controller/CustomerControllerTest.java | 22 +-- .../java/com/bankofspring/unittest/.gitkeep | 0 ...stomerControllerMockMvcStandaloneTest.java | 182 ++++++++++++++++++ 12 files changed, 243 insertions(+), 33 deletions(-) create mode 100644 src/main/resources/images/logo.png create mode 100644 src/test/java/com/bankofspring/BankOfSpringApplicationUnitTests.java rename src/test/java/com/bankofspring/{test => integrationtest}/.gitkeep (100%) rename src/test/java/com/bankofspring/{test => integrationtest}/controller/AccountControllerTest.java (92%) rename src/test/java/com/bankofspring/{test => integrationtest}/controller/CustomerControllerTest.java (89%) create mode 100644 src/test/java/com/bankofspring/unittest/.gitkeep create mode 100644 src/test/java/com/bankofspring/unittest/controller/CustomerControllerMockMvcStandaloneTest.java diff --git a/readme.md b/readme.md index 9ab322f..5689360 100644 --- a/readme.md +++ b/readme.md @@ -62,13 +62,18 @@ http://:8080/swagger-ui.html ```` ## Unit test cases -There are multiple unit test cases written to cover the different components of the application. However there is a global application test suite file _**BankOfSpringApplicationTests.java**_ that combines all the test cases in a logical manner to create a complete suite. It can be run from command prompt using the following command - +There are multiple unit test cases written to cover the different components of the application. However there is a global application test suite file _**BankOfSpringApplicationUnitTests.java**_ that combines all the test cases in a logical manner to create a complete suite. It can be run from command prompt using the following command - ```` -mvn clean test -Dtest=BankOfSpringApplicationTests +mvn clean test -Dtest=BankOfSpringApplicationUnitTests ```` ## Integration test cases +There are multiple integration test cases written to cover the different components of the application. However there is a global application test suite file _**BankOfSpringApplicationTests.java**_ that combines all the test cases in a logical manner to create a complete suite. It can be run from command prompt using the following command - + +```` +mvn clean test -Dtest=BankOfSpringApplicationTests +```` ## Contributors [Arpit Khandelwal](https://www.linkedin.com/in/arpitkhandelwal1984/) diff --git a/src/main/java/com/bankofspring/dto/CustomerDto.java b/src/main/java/com/bankofspring/dto/CustomerDto.java index 74b677b..c96c00b 100644 --- a/src/main/java/com/bankofspring/dto/CustomerDto.java +++ b/src/main/java/com/bankofspring/dto/CustomerDto.java @@ -20,8 +20,8 @@ @JsonIgnoreProperties(ignoreUnknown = true) public class CustomerDto { private Long customerId; - private String name; private String ssn; + private String name; private String address1; private String address2; private String city; diff --git a/src/main/java/com/bankofspring/exception/ApiError.java b/src/main/java/com/bankofspring/exception/ApiError.java index c47d0ec..6c57970 100644 --- a/src/main/java/com/bankofspring/exception/ApiError.java +++ b/src/main/java/com/bankofspring/exception/ApiError.java @@ -3,8 +3,12 @@ import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.DatabindContext; +import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; +import com.fasterxml.jackson.databind.type.SimpleType; +import com.fasterxml.jackson.databind.type.TypeFactory; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; @@ -14,6 +18,7 @@ import org.springframework.validation.ObjectError; import javax.validation.ConstraintViolation; +import java.io.IOException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -148,4 +153,5 @@ public String idFromValueAndType(Object value, Class suggestedType) { public JsonTypeInfo.Id getMechanism() { return JsonTypeInfo.Id.CUSTOM; } + } diff --git a/src/main/java/com/bankofspring/service/customer/CustomerServiceImpl.java b/src/main/java/com/bankofspring/service/customer/CustomerServiceImpl.java index 7472334..eae1ec1 100644 --- a/src/main/java/com/bankofspring/service/customer/CustomerServiceImpl.java +++ b/src/main/java/com/bankofspring/service/customer/CustomerServiceImpl.java @@ -20,10 +20,10 @@ @Service public class CustomerServiceImpl implements CustomerService { @Autowired - CustomerRepository customerRepository; + private CustomerRepository customerRepository; @Autowired - ModelMapper mapper; + private ModelMapper mapper; @Override public List getAllCustomers() { diff --git a/src/main/resources/images/logo.png b/src/main/resources/images/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..074004de9d5312a344f212ee9a60d935e95c6deb GIT binary patch literal 9677 zcmd6NcT`i|wy&QJqz36ls)F?1n}nuxDH4$0dyr5pdv-8hyhWgNbd#(q>CWM z0Fhn-B%y>7Aa8rl`Odq4+;PWv?~gl9#u_7g@60vZn)5f;nmb8mCi=8g98~Adouj>T zTgUv|x$}^7=lHPGLGI_Y+@LP*ym$PaJ>AXSo!!D8 z^tr2@J9oju+Y%BCF*Z_hh5Ac4|Dln9`v(HE&z)1#gaulSj03gZy}WNn1i4#8m{__-__`{)@oQ@Es=-wN2LA5B&b)AczW|sDT%G?P zd{uz!KaVB(dH+EY?5ocIPo*HnX1vyh%MN%>>EKDLyRstI2DJiY2 zto(;er5R_6ya{ig~3fyT!F zW*h+fm!kleNy422C8Z^#B>nyW*!2%;Sg^VK|5f9^rG{CC2f9m|yThO%L9W1hcnJJG z8SvfzEa(p*ppA-YkT&VD*j*ARdAfZ#hi>iobj5^mmZDq4!ligL;d(o#w?QVL4a z(n>NiTDpqbN^-h-3R*hSx_|TdZ|4FeU|q15qL!|Lw6v}gSXNP5TS;3{NlRKz5e%05 zd+wb8Sg><|tNY(_z5mGl=UnCgajuGXkh^m*G{_PP_4_*l%)Fq%P?#4qkXPG6f%m4h zcYqr-3?}?%dH!jwj(d=IsJok95Y(UdAO2PG{%_fAZL(>pp^mhfMjvsd!%L!8$Lt(a+T8QJNFpevug{jhzOfV6tgoNb=so09p`sZMv3&lrNq$w1QCn1p=S@&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Ig?{P}NWc)u%^Kr!C1L=;E=Ghl2XWPlCc@SAbrJUyHIdle7{1l~y~x4B}m zLWLA96K{49m@3*-`JlUqhup%ByC=qSP4x+=>oH?`Z`@x7$Gjw~4&-T_TX9U~J(ldZ=D)4RZa3zY$QCK7=JcVysXo6A!M?MY4{G+4sD^}uh4F04A9oE+#&k|u zQFMMWQ;~c2{&eh&kYH^s!!@{FAG&2L%BhE_28la%sAcpSH9y)LnP>|t2D;_k^MkWC1-b%?mR!)}fx2%SYdD{B5^|31=dHJb^*i%Fot3pB3L%}-B z!b3BuLq${OHsUJA=FX+KX|}<^h6V-ZuTMZ5cVc5>rQ~RO;z@-8rIMN*&byh&Z|9X> z@;4rc?8>Y84pPsFB1*_RKQKr8R0BBVn6QSRoGjZT_BUR~-xK;{pY_3QH{hV(Y!`sR z%6&AcD`35^sL3doMZ3k8+e_84`D8z=V7F7G!bvdWiU`}_t$$c}AJhfBwul~4sufiX z0;f{Mqi$8lMlH;E@2+x*L;du&$3m1eGMqH#n=E*?y`xsl z5TT>FYNaka=dqJ}bL2-T^vcetMRU?4r%I*t&fmWG3Qq`(5=u4h`+Rk+c7(q zVM7X_fizVZsZAes{pC7!e8taQ;)|<3Ym^0qn(zIoE0=)tdp$|NxRsoEQQ{?6;pzJK z4cH(hQ1aH$tBEr09$&zt-`Ro2tjC2A6gxjeOt9g}r$ClO#T!65%Jt_2-mv_a@yQJ_ zbp95lO&5pCU|f8ilFHBpfD@Kg!_Tbgq24NE%7qQL=nxNfmh@RoD)l|qVR@C2D>Ml! zT9*nOY56Xm`(GKiDF6D;93U{?#s5sr0Ifh^*kAuU>jOx6*u0B5PjfC+eCeX>M+d<_ Qf3UivYob#Lc6$7O0NXf0&j0`b literal 0 HcmV?d00001 diff --git a/src/test/java/com/bankofspring/BankOfSpringApplicationTests.java b/src/test/java/com/bankofspring/BankOfSpringApplicationTests.java index 082a4e1..f0029be 100644 --- a/src/test/java/com/bankofspring/BankOfSpringApplicationTests.java +++ b/src/test/java/com/bankofspring/BankOfSpringApplicationTests.java @@ -1,7 +1,7 @@ package com.bankofspring; -import com.bankofspring.test.controller.AccountControllerTest; -import com.bankofspring.test.controller.CustomerControllerTest; +import com.bankofspring.integrationtest.controller.AccountControllerTest; +import com.bankofspring.integrationtest.controller.CustomerControllerTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/src/test/java/com/bankofspring/BankOfSpringApplicationUnitTests.java b/src/test/java/com/bankofspring/BankOfSpringApplicationUnitTests.java new file mode 100644 index 0000000..d255c24 --- /dev/null +++ b/src/test/java/com/bankofspring/BankOfSpringApplicationUnitTests.java @@ -0,0 +1,17 @@ +package com.bankofspring; + + +import com.bankofspring.unittest.controller.CustomerControllerMockMvcStandaloneTest; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + CustomerControllerMockMvcStandaloneTest.class +}) +/** + * Created by Arpit Khandelwal. + */ +public class BankOfSpringApplicationUnitTests { +} diff --git a/src/test/java/com/bankofspring/test/.gitkeep b/src/test/java/com/bankofspring/integrationtest/.gitkeep similarity index 100% rename from src/test/java/com/bankofspring/test/.gitkeep rename to src/test/java/com/bankofspring/integrationtest/.gitkeep diff --git a/src/test/java/com/bankofspring/test/controller/AccountControllerTest.java b/src/test/java/com/bankofspring/integrationtest/controller/AccountControllerTest.java similarity index 92% rename from src/test/java/com/bankofspring/test/controller/AccountControllerTest.java rename to src/test/java/com/bankofspring/integrationtest/controller/AccountControllerTest.java index a22534c..c731c09 100644 --- a/src/test/java/com/bankofspring/test/controller/AccountControllerTest.java +++ b/src/test/java/com/bankofspring/integrationtest/controller/AccountControllerTest.java @@ -1,4 +1,4 @@ -package com.bankofspring.test.controller; +package com.bankofspring.integrationtest.controller; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; @@ -40,8 +40,8 @@ public void setup() { } /** - * Get all accounts test. - * All the accounts in the database should be returned as a result of this test and validation of total 10 accounts should pass. + * Get all accounts integrationtest. + * All the accounts in the database should be returned as a result of this integrationtest and validation of total 10 accounts should pass. * * @throws Exception */ @@ -55,7 +55,7 @@ public void ut1_GetAccounts() throws Exception{ } /** - * Get Account details test. + * Get Account details integrationtest. * Fetching the details for account with id 1 should be allowed and valid details should be returned. * * @throws Exception @@ -91,7 +91,7 @@ public void ut2_GetAccountByNumber() throws Exception { } /** - * Get Account by number test for invalid account number. + * Get Account by number integrationtest for invalid account number. * Trying to get an account's detail with invalid account number should result in a Http 400 error. * * @throws Exception @@ -107,8 +107,8 @@ public void ut3_GetAccountByNumber_InvalidAccount() throws Exception { } /** - * Create Account test. - * Account with details given in the test is created as a result of the test execution. + * Create Account integrationtest. + * Account with details given in the integrationtest is created as a result of the integrationtest execution. * * @throws Exception */ @@ -146,7 +146,7 @@ public void ut4_CreateAccount() throws Exception { } /** - * Create Account test for Invalid Customer. + * Create Account integrationtest for Invalid Customer. * Trying to create an account for a customer with id 100 should be disallowed. * A Http 400 should be thrown with message indicating that customer id 100 doesn't yet exist. * @@ -166,7 +166,7 @@ public void ut4_CreateAccount_InvalidCustomer() throws Exception { } /** - * Deposit Money test. + * Deposit Money integrationtest. * Account # 1 : Initial amount is $100 * A deposit of $50 should be allowed and resultant balance should be $150. * @@ -186,7 +186,7 @@ public void ut5_DepositMoney() throws Exception { } /** - * Withdraw Money test. + * Withdraw Money integrationtest. * Account # 1 : Amount after ut5 is $150 * A withdrawal of 50 should be allowed and new account balance should be $100. * @@ -206,7 +206,7 @@ public void ut6_WithdrawMoney() throws Exception { } /** - * Withdraw Money test for negative scenario. + * Withdraw Money integrationtest for negative scenario. * Account # 1 : Amount after ut6 is $100 * A withdrawal of $200 should raise a InsufficientFund Exception and send a Http 400 response. * @@ -226,10 +226,10 @@ public void ut7_WithdrawMoney_InsufficientFunds() throws Exception { } /** - * Transfer Money test. + * Transfer Money integrationtest. * Account # 1 : Initial Amount 100 * Account # 2 : Initial Amount 200 - * Once the test completes, $50 should be transferred from Account # 1 to Account # 2 + * Once the integrationtest completes, $50 should be transferred from Account # 1 to Account # 2 * * @throws Exception */ @@ -249,10 +249,10 @@ public void ut8_TransferMoney() throws Exception { } /** - * Transfer Money test to a non existent recipient. + * Transfer Money integrationtest to a non existent recipient. * Account # 1 : Initial Amount 100 * Account # 2 : doesn't exist - * Once the test completes, an Http 404 error should be returned indicating the recipient doesn't exist. + * Once the integrationtest completes, an Http 404 error should be returned indicating the recipient doesn't exist. * * @throws Exception */ diff --git a/src/test/java/com/bankofspring/test/controller/CustomerControllerTest.java b/src/test/java/com/bankofspring/integrationtest/controller/CustomerControllerTest.java similarity index 89% rename from src/test/java/com/bankofspring/test/controller/CustomerControllerTest.java rename to src/test/java/com/bankofspring/integrationtest/controller/CustomerControllerTest.java index 1e988bd..9f5b216 100644 --- a/src/test/java/com/bankofspring/test/controller/CustomerControllerTest.java +++ b/src/test/java/com/bankofspring/integrationtest/controller/CustomerControllerTest.java @@ -1,4 +1,4 @@ -package com.bankofspring.test.controller; +package com.bankofspring.integrationtest.controller; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; @@ -40,8 +40,8 @@ public void setup() { } /** - * Get all customers test. - * The test should result in returning the 5 customers which are present in the test database. + * Get all customers integrationtest. + * The integrationtest should result in returning the 5 customers which are present in the integrationtest database. * * @throws Exception */ @@ -55,8 +55,8 @@ public void ut1_GetAllCustomers() throws Exception{ } /** - * Get customer by ssn test. - * The test should result in returning customer's details for SSN:AK01 + * Get customer by ssn integrationtest. + * The integrationtest should result in returning customer's details for SSN:AK01 * * @throws Exception */ @@ -83,8 +83,8 @@ public void ut2_GetCustomerBySsn() throws Exception{ } /** - * Get customer by invalid ssn test. - * The test should result in returning a Http 404 for a customer with invalid ssn. + * Get customer by invalid ssn integrationtest. + * The integrationtest should result in returning a Http 404 for a customer with invalid ssn. * * @throws Exception */ @@ -99,8 +99,8 @@ public void ut3_GetCustomerBySsn_InvalidSsn() throws Exception { } /** - * Create customer test. - * The test should result in adding a new customer to the database with SSN:TK01 + * Create customer integrationtest. + * The integrationtest should result in adding a new customer to the database with SSN:TK01 * * @throws Exception */ @@ -130,8 +130,8 @@ public void ut4_CreateCustomer() throws Exception{ } /** - * Create duplicate customer test. - * Since a customer with SSN:TK01 has already been created in ut4, this test should result in Http 404. + * Create duplicate customer integrationtest. + * Since a customer with SSN:TK01 has already been created in ut4, this integrationtest should result in Http 404. * * @throws Exception */ diff --git a/src/test/java/com/bankofspring/unittest/.gitkeep b/src/test/java/com/bankofspring/unittest/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/bankofspring/unittest/controller/CustomerControllerMockMvcStandaloneTest.java b/src/test/java/com/bankofspring/unittest/controller/CustomerControllerMockMvcStandaloneTest.java new file mode 100644 index 0000000..8c72767 --- /dev/null +++ b/src/test/java/com/bankofspring/unittest/controller/CustomerControllerMockMvcStandaloneTest.java @@ -0,0 +1,182 @@ +package com.bankofspring.unittest.controller; + +import com.bankofspring.api.v1.controller.CustomerController; +import com.bankofspring.domain.model.Customer; +import com.bankofspring.dto.CustomerDto; +import com.bankofspring.exception.EntityNotFoundException; +import com.bankofspring.service.customer.CustomerService; +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * Created by Arpit Khandelwal. + */ +@RunWith(SpringRunner.class) +@WebMvcTest(CustomerController.class) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +public class CustomerControllerMockMvcStandaloneTest { + + @Autowired + private ModelMapper mapper; + + @Autowired + private MockMvc mockMvc; + + @MockBean + private CustomerService customerService; + + + @Before + public void setup(){} + + /** + * Get all customers unit-test. + * The unit test should result in returning the 2 customers which are provided by us as input. + * + * @throws Exception + */ + @Test + public void ut1_GetAllCustomers() throws Exception{ + List customers = new ArrayList<>(2); + customers.add(new Customer().setSsn("ssn1").setName("Cust1").setCity("London")); + customers.add(new Customer().setSsn("ssn2").setName("Cust2").setCity("London")); + + // given + doReturn(customers).when(customerService).getAllCustomers(); + + // when, then + mockMvc + .perform(MockMvcRequestBuilders.get("/v1/customer/").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(2))) + .andDo(print()); + } + + /** + * Get customer by ssn unit-test. + * The test should result in returning customer's details for SSN:AK01 as provided by us as input. + * + * @throws Exception + */ + @Test + public void ut2_GetCustomerBySsn() throws Exception{ + Customer customer = new Customer() + .setCustomerId(1L) + .setName("Arpit K") + .setSsn("AK01") + .setAddress1("VT1") + .setAddress2("Marine Bay1") + .setCity("SpringCity") + .setContactNumber("9425094250"); + + // given + doReturn(customer).when(customerService).getCustomer("AK01"); + + //when, then + mockMvc + .perform(MockMvcRequestBuilders.get("/v1/customer/AK01").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.customerId").exists()) + .andExpect(jsonPath("$.name").exists()) + .andExpect(jsonPath("$.ssn").exists()) + .andExpect(jsonPath("$.address1").exists()) + .andExpect(jsonPath("$.address2").exists()) + .andExpect(jsonPath("$.city").exists()) + .andExpect(jsonPath("$.contactNumber").exists()) + .andExpect(jsonPath("$.customerId").value(1)) + .andExpect(jsonPath("$.name").value("Arpit K")) + .andExpect(jsonPath("$.ssn").value("AK01")) + .andExpect(jsonPath("$.address1").value("VT1")) + .andExpect(jsonPath("$.address2").value("Marine Bay1")) + .andExpect(jsonPath("$.city").value("SpringCity")) + .andExpect(jsonPath("$.contactNumber").value("9425094250")) + .andDo(print()); + } + + /** + * Get customer by invalid ssn unit-test. + * The test should result in returning a Http 404 for a customer with invalid ssn. + * + * @throws Exception + */ + @Test + public void ut3_GetCustomerBySsn_InvalidSsn() throws Exception { + // given + doThrow(new EntityNotFoundException(Customer.class, "ssn", "AK02")).when(customerService).getCustomer("AK02"); + + // when, then + mockMvc + .perform(MockMvcRequestBuilders.get("/v1/customer/AK02").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.apierror.status").value("NOT_FOUND")) + .andExpect(jsonPath("$.apierror.message").value("Customer was not found for parameters {ssn=AK02}")) + .andDo(print()); + } + + /** + * Create customer unit-test. + * The test should result in adding a new customer with SSN:TK01 + * + * @throws Exception + */ + @Test + public void ut4_CreateCustomer() throws Exception{ + Customer customer = new Customer() + .setCustomerId(1L) + .setName("Test Customer") + .setSsn("TK01") + .setAddress1("Unit-Test") + .setAddress2("Spring-Boot") + .setCity("SpringCity") + .setContactNumber("9425094250"); + + // given + doReturn(customer).when(customerService).createCustomer(mapper.map(customer, CustomerDto.class)); + + // when, then + mockMvc + .perform(MockMvcRequestBuilders.post("/v1/customer/create") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"name\":\"Test Customer\", \"ssn\":\"TK01\", \"contactNumber\":\"9425094250\",\"address1\":\"Unit-Test\",\"address2\":\"Spring-Boot\",\"city\":\"SpringCity\"}") + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.customerId").exists()) + .andExpect(jsonPath("$.name").exists()) + .andExpect(jsonPath("$.ssn").exists()) + .andExpect(jsonPath("$.address1").exists()) + .andExpect(jsonPath("$.address2").exists()) + .andExpect(jsonPath("$.city").exists()) + .andExpect(jsonPath("$.contactNumber").exists()) + .andExpect(jsonPath("$.customerId").value(1)) + .andExpect(jsonPath("$.name").value("Test Customer")) + .andExpect(jsonPath("$.ssn").value("TK01")) + .andExpect(jsonPath("$.address1").value("Unit-Test")) + .andExpect(jsonPath("$.address2").value("Spring-Boot")) + .andExpect(jsonPath("$.city").value("SpringCity")) + .andExpect(jsonPath("$.contactNumber").value("9425094250")) + .andDo(print()); + } + + +}