Skip to content

Commit

Permalink
Merge pull request #26 from nhkhai/SIS-24
Browse files Browse the repository at this point in the history
SIS-24: Added dirties context for integration test cases.
  • Loading branch information
nhkhai authored Mar 30, 2024
2 parents 6724e74 + df9b565 commit 05f3284
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +34,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class CustomerControllerTest {
@Autowired
private MockMvc mockMvc;
Expand Down Expand Up @@ -75,7 +79,7 @@ public void createCustomerTest() throws Exception {
.address("123 HK St").postalCode(654321).phoneNumber(87654321)
.email("[email protected]").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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +33,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +40,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class ReviewControllerTest {
@Autowired
private MockMvc mockMvc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +42,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class IntegrationTest {
@Mock
private CustomerRepository customerRepository;
Expand Down Expand Up @@ -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("[email protected]").build();
Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong")
.address("123 HK St").postalCode(654321).phoneNumber(87654321)
.email("[email protected]").build();

Customer testObject2 = Customer.builder().firstName("Jack").lastName("Chang").country("China")
.address("321 HK St").postalCode(123456).phoneNumber(12345678)
.email("[email protected]").build();

// Step 2: Convert the Java objects to JSON using ObjectMapper.
String testObject1AsJSON = objectMapper.writeValueAsString(testObject1);
Expand All @@ -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() + ". ");
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/sg/com/smartinventory/useCases/UseCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +42,7 @@

@SpringBootTest
@AutoConfigureMockMvc
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class UseCaseTest {
@Mock
private CustomerRepository customerRepository;
Expand Down Expand Up @@ -115,9 +118,9 @@ public void runIntegratedTest() throws Exception {
.address("123 HK St").postalCode(654321).phoneNumber(87654321)
.email("[email protected]").build();

Customer testObject2 = Customer.builder().firstName("Jackie").lastName("Chang").country("Hong Kong")
.address("123 HK St").postalCode(654321).phoneNumber(87654321)
.email("jackie[email protected]").build();
Customer testObject2 = Customer.builder().firstName("Jack").lastName("Chang").country("China")
.address("321 HK St").postalCode(123456).phoneNumber(12345678)
.email("jack[email protected]").build();

// Step 2: Convert the Java objects to JSON using ObjectMapper.
String testObject1AsJSON = objectMapper.writeValueAsString(testObject1);
Expand All @@ -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() + ". ");
Expand Down

0 comments on commit 05f3284

Please sign in to comment.