Skip to content

Commit

Permalink
use @DisplayName on the test
Browse files Browse the repository at this point in the history
  • Loading branch information
WeRockStar committed Mar 22, 2024
1 parent cd9ea10 commit 5ee4938
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 deletions.
36 changes: 15 additions & 21 deletions kbazaar/src/test/java/com/kampus/kbazaar/KBazaarApplicationIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -18,7 +15,6 @@
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.testcontainers.containers.PostgreSQLContainer;

@SpringBootTest
Expand Down Expand Up @@ -55,28 +51,26 @@ static void stopContainer() {
}

@Test
@DisplayName("should return shopper list")
void getShopper_shouldReturnShopperList() throws Exception {
MvcResult mvcResult =
mockMvc.perform(
get("/api/v1/shoppers")
.header("Authorization", "Bearer " + jwtToken))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.length()").value(Matchers.greaterThan(0)))
.andExpect(jsonPath("$[0].username").value("TechNinja"))
.andReturn();
mockMvc.perform(get("/api/v1/shoppers").header("Authorization", "Bearer " + jwtToken))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$.length()").value(Matchers.greaterThan(0)))
.andExpect(jsonPath("$[0].username").value("TechNinja"))
.andReturn();
}

@Test
@DisplayName("should return shopper by username")
void getShopperByName_shouldReturnShopper() throws Exception {
String username = "TechNinja";

MvcResult mvcResult =
mockMvc.perform(
get("/api/v1/shoppers/" + username)
.header("Authorization", "Bearer " + jwtToken))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username").value(username))
.andReturn();
mockMvc.perform(
get("/api/v1/shoppers/" + username)
.header("Authorization", "Bearer " + jwtToken))
.andExpect(status().isOk())
.andExpect(jsonPath("$.username").value(username))
.andReturn();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -27,7 +28,8 @@ void setUp() {
}

@Test
void testShouldBeAbleToGetAllProducts() {
@DisplayName("should be able to get all products")
void shouldBeAbleToGetAllProducts() {
// Mock data
Product product1 =
new Product(
Expand All @@ -53,7 +55,8 @@ void testShouldBeAbleToGetAllProducts() {
}

@Test
void testShouldReturnEmptyListWhenNoProductFoundGetAllProducts() {
@DisplayName("should return empty list when no product found")
void shouldReturnEmptyListWhenNoProductFoundGetAllProducts() {
// Mock repository method returning empty list
when(productRepository.findAll()).thenReturn(Arrays.asList());

Expand All @@ -65,7 +68,8 @@ void testShouldReturnEmptyListWhenNoProductFoundGetAllProducts() {
}

@Test
void testShouldBeAbleToGetProductBySku() {
@DisplayName("should be able to get product by SKU")
void shouldBeAbleToGetProductBySku() {
// Mock data
Product product =
new Product(1L, "Pens", "STATIONERY-PEN-BIC-BALLPOINT", new BigDecimal(14.99), 100);
Expand All @@ -83,7 +87,8 @@ void testShouldBeAbleToGetProductBySku() {
}

@Test
void testShouldReturnNullWhenGetProductNonExistingSKU() {
@DisplayName("should return null when get product non-existing SKU")
void shouldReturnNullWhenGetProductNonExistingSKU() {
// Mock repository method returning empty optional
when(productRepository.findBySku(anyString())).thenReturn(Optional.empty());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -25,7 +26,8 @@ void setUp() {
}

@Test
void testGetAll() {
@DisplayName("should be able to get all promotions")
void shouldBeAbleToGetAllPromotion() {
// Arrange
Promotion promotion1 = new Promotion();
Promotion promotion2 = new Promotion();
Expand All @@ -39,7 +41,8 @@ void testGetAll() {
}

@Test
void testGetPromotionByCode_ExistingCode_ShouldReturnPromotionResponse() {
@DisplayName("should be able to get promotion by code")
void getPromotionByCode_ExistingCode_ShouldReturnPromotionResponse() {
// Arrange
String code = "BUY2GET1FREE";
Promotion promotion = new Promotion();
Expand All @@ -55,7 +58,8 @@ void testGetPromotionByCode_ExistingCode_ShouldReturnPromotionResponse() {
}

@Test
void testGetPromotionByCode_NonExistingCode_ShouldThrowNotFoundException() {
@DisplayName("should throw NotFoundException when promotion code not found")
void getPromotionByCode_NonExistingCode_ShouldThrowNotFoundException() {
// Arrange
String code = "NON-EXISTING-CODE";
when(promotionRepository.findByCode(code)).thenReturn(Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand All @@ -25,7 +26,8 @@ void setUp() {
}

@Test
void testGetAll_ShouldReturnListOfShoppers() {
@DisplayName("should be able to get all shoppers")
void getAll_ShouldReturnListOfShoppers() {
// Mock data
Shopper shopper1 = new Shopper(1L, "TechNinja", "[email protected]");
Shopper shopper2 = new Shopper(2L, "CodeMaster", "[email protected]");
Expand All @@ -44,7 +46,8 @@ void testGetAll_ShouldReturnListOfShoppers() {
}

@Test
void testGetById_ShouldReturnShopper() {
@DisplayName("should be able to get shopper by id")
void getShopperById_ShouldReturnShopper() {
// Mock data
Shopper shopper = new Shopper(1L, "DataGuru", "[email protected]");

Expand All @@ -59,7 +62,8 @@ void testGetById_ShouldReturnShopper() {
}

@Test
void testGetById_ShouldThrowNotFoundException() {
@DisplayName("should throw NotFoundException when shopper not found")
void getShopperById_ShouldThrowNotFoundException() {
// Mock repository method returning empty optional
when(shopperRepository.findById(1)).thenReturn(Optional.empty());

Expand All @@ -68,7 +72,8 @@ void testGetById_ShouldThrowNotFoundException() {
}

@Test
void testGetByUsername_ShouldReturnShopper() {
@DisplayName("should be able to get shopper by username")
void getShopperByUsername_ShouldReturnShopper() {
// Mock data
Shopper shopper = new Shopper(1L, "DataGuru", "[email protected]");

Expand All @@ -83,7 +88,8 @@ void testGetByUsername_ShouldReturnShopper() {
}

@Test
void testGetByUsername_ShouldThrowNotFoundException() {
@DisplayName("should throw NotFoundException when shopper not found")
void getByUsername_ShouldThrowNotFoundException() {
// Mock repository method returning empty optional
when(shopperRepository.findByUsername("DataGuru")).thenReturn(Optional.empty());

Expand Down

0 comments on commit 5ee4938

Please sign in to comment.