test(restaurant): ajoute les tests unitaires pour le CRUD. Correction du test Personne et du main
This commit is contained in:
parent
9a55149802
commit
4402a2ae2b
5 changed files with 283 additions and 2 deletions
|
@ -7,7 +7,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
|||
class MangerAutourDeSimplonBackApplicationTests {
|
||||
|
||||
@Test
|
||||
void testMain() {
|
||||
MangerAutourDeSimplonBackApplication.main(new String[] {});
|
||||
void contextLoads() {
|
||||
// Ce test garantit que le contexte de l'application Spring peut démarrer avec
|
||||
// succès.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,120 @@
|
|||
package fr.vincent.ramiere.mangerautourdesimplonback.controller;
|
||||
|
||||
import fr.vincent.ramiere.mangerautourdesimplonback.models.Restaurant;
|
||||
import fr.vincent.ramiere.mangerautourdesimplonback.service.RestaurantService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class RestaurantControllerTest {
|
||||
|
||||
@Mock
|
||||
private RestaurantService restaurantService;
|
||||
|
||||
@InjectMocks
|
||||
private RestaurantController restaurantController;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllRestaurants_shouldReturnListOfRestaurants() {
|
||||
Restaurant r1 = new Restaurant();
|
||||
Restaurant r2 = new Restaurant();
|
||||
List<Restaurant> restaurants = Arrays.asList(r1, r2);
|
||||
|
||||
when(restaurantService.getAllRestaurants()).thenReturn(restaurants);
|
||||
|
||||
ResponseEntity<List<Restaurant>> response = restaurantController.getAllRestaurants();
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(restaurants, response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRestaurantById_found_shouldReturnRestaurant() {
|
||||
Restaurant restaurant = new Restaurant();
|
||||
when(restaurantService.getRestaurantById(1)).thenReturn(Optional.of(restaurant));
|
||||
|
||||
ResponseEntity<Restaurant> response = restaurantController.getRestaurantById(1);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(restaurant, response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRestaurantById_notFound_shouldReturnNotFound() {
|
||||
when(restaurantService.getRestaurantById(1)).thenReturn(Optional.empty());
|
||||
|
||||
ResponseEntity<Restaurant> response = restaurantController.getRestaurantById(1);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertNull(response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createRestaurant_shouldReturnCreatedRestaurant() {
|
||||
Restaurant restaurant = new Restaurant();
|
||||
Restaurant savedRestaurant = new Restaurant();
|
||||
when(restaurantService.saveRestaurant(restaurant)).thenReturn(savedRestaurant);
|
||||
|
||||
ResponseEntity<Restaurant> response = restaurantController.createRestaurant(restaurant);
|
||||
|
||||
assertEquals(HttpStatus.CREATED, response.getStatusCode());
|
||||
assertEquals(savedRestaurant, response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateRestaurant_found_shouldReturnUpdatedRestaurant() {
|
||||
Restaurant details = new Restaurant();
|
||||
Restaurant updated = new Restaurant();
|
||||
when(restaurantService.updateRestaurant(eq(1), any(Restaurant.class))).thenReturn(Optional.of(updated));
|
||||
|
||||
ResponseEntity<Restaurant> response = restaurantController.updateRestaurant(1, details);
|
||||
|
||||
assertEquals(HttpStatus.OK, response.getStatusCode());
|
||||
assertEquals(updated, response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateRestaurant_notFound_shouldReturnNotFound() {
|
||||
Restaurant details = new Restaurant();
|
||||
when(restaurantService.updateRestaurant(eq(1), any(Restaurant.class))).thenReturn(Optional.empty());
|
||||
|
||||
ResponseEntity<Restaurant> response = restaurantController.updateRestaurant(1, details);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
assertNull(response.getBody());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRestaurant_found_shouldReturnNoContent() {
|
||||
when(restaurantService.getRestaurantById(1)).thenReturn(Optional.of(new Restaurant()));
|
||||
|
||||
ResponseEntity<Void> response = restaurantController.deleteRestaurant(1);
|
||||
|
||||
assertEquals(HttpStatus.NO_CONTENT, response.getStatusCode());
|
||||
verify(restaurantService, times(1)).deleteRestaurant(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteRestaurant_notFound_shouldReturnNotFound() {
|
||||
when(restaurantService.getRestaurantById(1)).thenReturn(Optional.empty());
|
||||
|
||||
ResponseEntity<Void> response = restaurantController.deleteRestaurant(1);
|
||||
|
||||
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
|
||||
verify(restaurantService, never()).deleteRestaurant(anyInt());
|
||||
}
|
||||
}
|
|
@ -9,5 +9,8 @@ class PersonneTest {
|
|||
void testBuilder() {
|
||||
Personne p = Personne.builder().nom("Test").prenom("Test").email("t@t.fr").password("1234").build();
|
||||
assertEquals("Test", p.getNom());
|
||||
assertEquals("Test", p.getPrenom());
|
||||
assertEquals("t@t.fr", p.getEmail());
|
||||
assertEquals("1234", p.getPassword());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package fr.vincent.ramiere.mangerautourdesimplonback.models;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RestaurantTest {
|
||||
@Test
|
||||
void testBuilder() {
|
||||
Restaurant r = Restaurant.builder().nom("Test").adresse("Adresse Test").telephone("0123456789").accesPMR(true)
|
||||
.aEmporter(true)
|
||||
.surPlace(false).prix(10).latitude("1.0").longitude("2.0").website("site-test.com").build();
|
||||
assertEquals("Test", r.getNom());
|
||||
assertEquals("Adresse Test", r.getAdresse());
|
||||
assertEquals("0123456789", r.getTelephone());
|
||||
assertTrue(r.getAccesPMR());
|
||||
assertTrue(r.getAEmporter());
|
||||
assertFalse(r.getSurPlace());
|
||||
assertEquals(10, r.getPrix());
|
||||
assertEquals("1.0", r.getLatitude());
|
||||
assertEquals("2.0", r.getLongitude());
|
||||
assertEquals("site-test.com", r.getWebsite());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
package fr.vincent.ramiere.mangerautourdesimplonback.services;
|
||||
|
||||
import fr.vincent.ramiere.mangerautourdesimplonback.models.Restaurant;
|
||||
import fr.vincent.ramiere.mangerautourdesimplonback.repository.RestaurantRepository;
|
||||
import fr.vincent.ramiere.mangerautourdesimplonback.service.RestaurantService;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class RestaurantServiceTest {
|
||||
@Mock
|
||||
private RestaurantRepository restaurantRepository;
|
||||
@InjectMocks
|
||||
private RestaurantService restaurantService;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
MockitoAnnotations.openMocks(this);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetAllRestaurants() {
|
||||
Restaurant r1 = new Restaurant();
|
||||
Restaurant r2 = new Restaurant();
|
||||
when(restaurantRepository.findAll()).thenReturn(Arrays.asList(r1, r2));
|
||||
|
||||
List<Restaurant> result = restaurantService.getAllRestaurants();
|
||||
|
||||
assertEquals(2, result.size());
|
||||
verify(restaurantRepository).findAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRestaurantById_Found() {
|
||||
Restaurant r = new Restaurant();
|
||||
r.setNom("Test");
|
||||
when(restaurantRepository.findById(1)).thenReturn(Optional.of(r));
|
||||
|
||||
Optional<Restaurant> result = restaurantService.getRestaurantById(1);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertEquals("Test", result.get().getNom());
|
||||
verify(restaurantRepository).findById(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetRestaurantById_NotFound() {
|
||||
when(restaurantRepository.findById(2)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<Restaurant> result = restaurantService.getRestaurantById(2);
|
||||
|
||||
assertFalse(result.isPresent());
|
||||
verify(restaurantRepository).findById(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSaveRestaurant() {
|
||||
Restaurant r = new Restaurant();
|
||||
r.setNom("SaveTest");
|
||||
when(restaurantRepository.save(r)).thenReturn(r);
|
||||
|
||||
Restaurant result = restaurantService.saveRestaurant(r);
|
||||
|
||||
assertEquals("SaveTest", result.getNom());
|
||||
verify(restaurantRepository).save(r);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRestaurant_Found() {
|
||||
Restaurant existing = new Restaurant();
|
||||
existing.setNom("Old");
|
||||
Restaurant details = new Restaurant();
|
||||
details.setNom("New");
|
||||
details.setAdresse("Adresse");
|
||||
details.setTelephone("123");
|
||||
details.setAccesPMR(true);
|
||||
details.setAEmporter(true);
|
||||
details.setSurPlace(false);
|
||||
details.setPrix(10);
|
||||
details.setLatitude("1.0");
|
||||
details.setLongitude("2.0");
|
||||
details.setWebsite("site");
|
||||
|
||||
when(restaurantRepository.findById(1)).thenReturn(Optional.of(existing));
|
||||
when(restaurantRepository.save(any(Restaurant.class))).thenAnswer(invocation -> invocation.getArgument(0));
|
||||
|
||||
Optional<Restaurant> result = restaurantService.updateRestaurant(1, details);
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
Restaurant updatedRestaurant = result.get();
|
||||
assertEquals("New", updatedRestaurant.getNom());
|
||||
assertEquals("Adresse", updatedRestaurant.getAdresse());
|
||||
assertEquals("123", updatedRestaurant.getTelephone());
|
||||
assertTrue(updatedRestaurant.getAccesPMR());
|
||||
assertTrue(updatedRestaurant.getAEmporter());
|
||||
assertFalse(updatedRestaurant.getSurPlace());
|
||||
assertEquals(10, updatedRestaurant.getPrix());
|
||||
assertEquals("1.0", updatedRestaurant.getLatitude());
|
||||
assertEquals("2.0", updatedRestaurant.getLongitude());
|
||||
assertEquals("site", updatedRestaurant.getWebsite());
|
||||
verify(restaurantRepository).findById(1);
|
||||
verify(restaurantRepository).save(existing);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateRestaurant_NotFound() {
|
||||
Restaurant details = new Restaurant();
|
||||
when(restaurantRepository.findById(99)).thenReturn(Optional.empty());
|
||||
|
||||
Optional<Restaurant> result = restaurantService.updateRestaurant(99, details);
|
||||
|
||||
assertFalse(result.isPresent());
|
||||
verify(restaurantRepository).findById(99);
|
||||
verify(restaurantRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDeleteRestaurant() {
|
||||
restaurantService.deleteRestaurant(5);
|
||||
verify(restaurantRepository).deleteById(5);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue