-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing Unit Tests with Mockito
- Loading branch information
1 parent
495dc6e
commit e19d20f
Showing
5 changed files
with
160 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,8 @@ | |
@SpringBootTest | ||
class BookingSystemRestApiApplicationTests { | ||
|
||
@Test | ||
/*@Test | ||
void contextLoads() { | ||
} | ||
}*/ | ||
|
||
} |
81 changes: 81 additions & 0 deletions
81
...java/com/davidorellana/bookingsystemrestapi/booking/controller/BookingControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...t/java/com/davidorellana/bookingsystemrestapi/booking/service/BookingServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters