Skip to content

Commit

Permalink
Implementing Unit Tests with Mockito
Browse files Browse the repository at this point in the history
  • Loading branch information
davidorellana98 committed Dec 27, 2022
1 parent 495dc6e commit e19d20f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
@SpringBootTest
class BookingSystemRestApiApplicationTests {

@Test
/*@Test
void contextLoads() {
}
}*/

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.davidorellana.bookingsystemrestapi.booking.controller;

import com.davidorellana.bookingsystemrestapi.booking.model.data.Booking;
import com.davidorellana.bookingsystemrestapi.booking.model.data.PaymentMethods;
import com.davidorellana.bookingsystemrestapi.booking.repository.mongorepository.BookingMongoRepository;
import com.davidorellana.bookingsystemrestapi.booking.service.BookingService;
import com.davidorellana.bookingsystemrestapi.user.model.data.User;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class BookingControllerTest {

@Mock
private BookingService bookingService;

@InjectMocks
private BookingController bookingController;

@Test
@Order(1)
public void delete_booking_by_id_200_ok() {

Mockito.when(bookingService.deleteBookingById(Mockito.anyString())).thenReturn(true);

ResponseEntity<Boolean> bookingResultController = bookingController.deleteBookingById("1");
Assertions.assertEquals(200, bookingResultController.getStatusCodeValue());
}

@Test
@Order(2)
public void delete_booking_by_id_400_not_found() {

Mockito.when(bookingService.deleteBookingById(Mockito.anyString())).thenReturn(false);

ResponseEntity<Boolean> bookingResultController = bookingController.deleteBookingById("1");
Assertions.assertEquals(404, bookingResultController.getStatusCodeValue());
}

@Test
@Order(3)
public void find_bookings_by_bookingType_200_ok() {

List<Booking> bookingList = new ArrayList<>();
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,30), PaymentMethods.CASH, 2,30.0,60.0));
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,31), PaymentMethods.ALIPAY, 3,10.0,30.0));

Mockito.when(bookingService.findBookingsByBookingType("home")).thenReturn(bookingList);

ResponseEntity<List<Booking>> bookingListResultController = bookingController.findBookingsByBookingType("home");
Assertions.assertEquals(200, bookingListResultController.getStatusCodeValue());
}

@Test
@Order(4)
public void find_bookings_by_paymentMethods_200_ok() {

List<Booking> bookingList = new ArrayList<>();
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,30), PaymentMethods.CASH, 2,30.0,60.0));
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,31), PaymentMethods.ALIPAY, 3,10.0,30.0));


Mockito.when(bookingService.findBookingsByPaymentMethods("CASH")).thenReturn(bookingList);

ResponseEntity<List<Booking>> bookingListResultController = bookingController.findBookingsByPaymentMethods("CASH");
Assertions.assertEquals(200, bookingListResultController.getStatusCodeValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.davidorellana.bookingsystemrestapi.booking.service;

import com.davidorellana.bookingsystemrestapi.booking.model.data.Booking;
import com.davidorellana.bookingsystemrestapi.booking.model.data.PaymentMethods;
import com.davidorellana.bookingsystemrestapi.booking.repository.BookingRepositoryDao;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class BookingServiceImplTest {

@Mock
private BookingRepositoryDao bookingRepositoryDao;

@InjectMocks
private BookingServiceImpl bookingService;

@Test
@Order(1)
public void delete_booking_by_id() {

Mockito.when(bookingRepositoryDao.deleteBookingById("1")).thenReturn(true);

Boolean deleteBookingResult = bookingService.deleteBookingById("1");

Assertions.assertEquals(true, deleteBookingResult);
}

@Test
@Order(2)
public void find_bookings_by_bookingType() {

List<Booking> bookingList = new ArrayList<>();
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,30), PaymentMethods.CASH, 2,30.0,60.0));
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,31), PaymentMethods.ALIPAY, 3,10.0,30.0));

Mockito.when(bookingRepositoryDao.findBookingsByBookingType("home")).thenReturn(bookingList);

List<Booking> findBookingAllResult = bookingService.findBookingsByBookingType(bookingList.listIterator().next().getBookingType());
List<Booking> bookingListExpected = new ArrayList<>();
bookingListExpected.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,30), PaymentMethods.CASH, 2,30.0,60.0));
bookingListExpected.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,31), PaymentMethods.ALIPAY, 3,10.0,30.0));


Assertions.assertEquals(bookingListExpected, findBookingAllResult);
}

@Test
@Order(2)
public void find_bookings_by_paymentMethods() {

List<Booking> bookingList = new ArrayList<>();
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,30), PaymentMethods.CASH, 2,30.0,60.0));
bookingList.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,31), PaymentMethods.ALIPAY, 3,10.0,30.0));

Mockito.when(bookingRepositoryDao.findBookingsByPaymentMethods("CASH")).thenReturn(bookingList);

List<Booking> findBookingPaymentResult = bookingService.findBookingsByPaymentMethods(bookingList.listIterator().next().getPaymentMethods().toString());
List<Booking> bookingPaymentExpected = new ArrayList<>();
bookingPaymentExpected.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,30), PaymentMethods.CASH, 2,30.0,60.0));
bookingPaymentExpected.add(new Booking("home", true, LocalDate.now(), LocalDate.of(2022,12,31), PaymentMethods.ALIPAY, 3,10.0,30.0));


Assertions.assertEquals(bookingPaymentExpected, findBookingPaymentResult);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.davidorellana.bookingsystemrestapi.user.controller;

import com.davidorellana.bookingsystemrestapi.user.model.data.User;
import com.davidorellana.bookingsystemrestapi.user.model.dto.UserDto;
import com.davidorellana.bookingsystemrestapi.user.model.dto.UserUpdatedDto;
import com.davidorellana.bookingsystemrestapi.user.service.UserService;
import org.junit.jupiter.api.*;
Expand All @@ -15,8 +14,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class UserControllerTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class UserServiceImplTest {
Expand Down

0 comments on commit e19d20f

Please sign in to comment.