Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more tests to increase the test coverage #28

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.NoSuchElementException;

@RestController
@AllArgsConstructor
Expand All @@ -22,12 +21,8 @@ public List<ShoppingList> getAllLists(){
}

@GetMapping("/{id}")
public ShoppingList getListById(@PathVariable String id){
ShoppingList shoppingList = shoppingListService.getListById(id);
if (shoppingList == null) {
throw new NoSuchElementException("List not found");
}
return shoppingList;
public ShoppingList getListById(@PathVariable String id) {
return shoppingListService.getListById(id);
}

@PostMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public List<ShoppingList> getAllLists() {
}

public ShoppingList getListById(String id) {
return shoppingListRepository.findById(id).orElse(null);

return shoppingListRepository.findById(id).orElseThrow(() -> new NoSuchElementException("List not found"));
}

public ShoppingList createShoppingList(ShoppingList shoppingList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -46,7 +44,7 @@ void getAllLists_shouldReturnAllLists() throws Exception {
Product product2 = new Product("cheese", 10);
List<Product> products = List.of(product1, product2);
ShoppingList shoppingList = new ShoppingList("1", "new 1", "make smth", products);
when(shoppingListRepository.findAll()).thenReturn(Arrays.asList(shoppingList));
when(shoppingListRepository.findAll()).thenReturn(List.of(shoppingList));

mockMvc.perform(MockMvcRequestBuilders.get("/api/lists"))
.andExpect(status().isOk())
Expand Down Expand Up @@ -90,7 +88,8 @@ void getListById_shouldReturnListById() throws Exception {
@Test
@DirtiesContext
void getListById_shouldReturnNotFoundIfListDoesNotExist() throws Exception {
when(shoppingListService.getListById("2")).thenThrow(new NoSuchElementException("List not found"));
when(shoppingListRepository.findById("2")).thenReturn(Optional.empty());

mockMvc.perform(MockMvcRequestBuilders.get("/api/lists/2"))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.message").value("List not found"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.backend.exception;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@WebMvcTest(GlobalExceptionHandler.class)
public class GlobalExceptionHandlerTest {

@Autowired
private MockMvc mockMvc;

@Autowired
private WebApplicationContext webApplicationContext;

@BeforeEach
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.build();
}

@Test
public void testHandleUnexpectedExceptions() throws Exception {
mockMvc.perform(get("/some-endpoint-that-throws-exception"))
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value(GlobalExceptionHandler.INTERNAL_SERVER_ERROR_MESSAGE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ public void getListById_shouldReturnListById() {
}

@Test
public void getListById_shouldReturnNullIfNotFound() {
public void getListById_shouldThrowNoSuchElementExceptionIfNotFound() {
// GIVEN
when(repository.findById("2")).thenReturn(Optional.empty());
// WHEN
ShoppingList result = service.getListById("2");
// THEN
assertNull(result);

// WHEN & THEN
assertThrows(NoSuchElementException.class, () -> {
service.getListById("2");
});
}

@Test
Expand Down