diff --git a/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java index 210fa73..ec28bdb 100644 --- a/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/CustomerControllerTest.java @@ -21,6 +21,9 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; + import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -31,6 +34,7 @@ @SpringBootTest @AutoConfigureMockMvc +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class CustomerControllerTest { @Autowired private MockMvc mockMvc; @@ -75,7 +79,7 @@ public void createCustomerTest() throws Exception { .address("123 HK St").postalCode(654321).phoneNumber(87654321) .email("jackie.chan@example.com").build(); - // Step 2: Convert the Java object to JSON using ObjectMapper. + // Step 2: Convert the Java objects to JSON using ObjectMapper. String newCustomerAsJSON = objectMapper.writeValueAsString(newCustomer); // Step 3: Build the request. diff --git a/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java index ed5fe1e..457f1e6 100644 --- a/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/ProductControllerTest.java @@ -21,6 +21,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -31,6 +33,7 @@ @SpringBootTest @AutoConfigureMockMvc +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class ProductControllerTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java index 1b3afc0..75fda21 100644 --- a/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java +++ b/src/test/java/sg/com/smartinventory/controllers/ReviewControllerTest.java @@ -25,6 +25,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -38,6 +40,7 @@ @SpringBootTest @AutoConfigureMockMvc +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class ReviewControllerTest { @Autowired private MockMvc mockMvc; diff --git a/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java b/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java index 7d2083d..bcbea1e 100644 --- a/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java +++ b/src/test/java/sg/com/smartinventory/integration/IntegrationTest.java @@ -28,6 +28,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -40,6 +42,7 @@ @SpringBootTest @AutoConfigureMockMvc +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class IntegrationTest { @Mock private CustomerRepository customerRepository; @@ -114,9 +117,10 @@ public void runIntegratedTest() throws Exception { Customer testObject1 = Customer.builder().firstName("Jackie").lastName("Chan").country("Hong Kong") .address("123 HK St").postalCode(654321).phoneNumber(87654321) .email("jackie.chan@example.com").build(); - Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong") - .address("123 HK St").postalCode(654321).phoneNumber(87654321) - .email("jackie.chang@example.com").build(); + + Customer testObject2 = Customer.builder().firstName("Jack").lastName("Chang").country("China") + .address("321 HK St").postalCode(123456).phoneNumber(12345678) + .email("jack.chang@example.com").build(); // Step 2: Convert the Java objects to JSON using ObjectMapper. String testObject1AsJSON = objectMapper.writeValueAsString(testObject1); @@ -142,7 +146,7 @@ public void runIntegratedTest() throws Exception { .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.firstName").value("Jackie")) + .andExpect(jsonPath("$.firstName").value("Jack")) .andExpect(jsonPath("$.lastName").value("Chang")); test_logger.info("Ending test: " + getCurrentMethodName() + ". "); diff --git a/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java b/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java index 319c1cc..14c6dc4 100644 --- a/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java +++ b/src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java @@ -28,6 +28,8 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.annotation.DirtiesContext.ClassMode; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.RequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -40,6 +42,7 @@ @SpringBootTest @AutoConfigureMockMvc +@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) public class UseCaseTest { @Mock private CustomerRepository customerRepository; @@ -115,9 +118,9 @@ public void runIntegratedTest() throws Exception { .address("123 HK St").postalCode(654321).phoneNumber(87654321) .email("jackie.chan@example.com").build(); - Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong") - .address("123 HK St").postalCode(654321).phoneNumber(87654321) - .email("jackie.chang@example.com").build(); + Customer testObject2 = Customer.builder().firstName("Jack").lastName("Chang").country("China") + .address("321 HK St").postalCode(123456).phoneNumber(12345678) + .email("jack.chang@example.com").build(); // Step 2: Convert the Java objects to JSON using ObjectMapper. String testObject1AsJSON = objectMapper.writeValueAsString(testObject1); @@ -144,7 +147,7 @@ public void runIntegratedTest() throws Exception { .andDo(print()) .andExpect(status().isCreated()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.firstName").value("Jackie")) + .andExpect(jsonPath("$.firstName").value("Jack")) .andExpect(jsonPath("$.lastName").value("Chang")); test_logger.info("Ending test: " + getCurrentMethodName() + ". ");