firedingo

Java Unit Test Code

Sep 24th, 2020
1,124
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LoanTest.java
  2. ----------------------------------------------------------------------------
  3. @ExtendWith(MockitoExtension.class)
  4. class LoanTest {
  5.    
  6.     /* commit
  7.      * CheckOverdueDate*
  8.      */
  9.    
  10.     ILoan loan;
  11.     int loanId;
  12.     Date dueDate;
  13.    
  14.     @Test
  15.     void CommitANonPendingLoan() {
  16.         //Arrange
  17.        
  18.         //Act
  19.         RuntimeException exception = loan.commit(loanId, dueDate);
  20.         //Assert
  21.         assertThrows(RuntimeException.class, exception);
  22.     }
  23. ------------------------------------------------------------------------------------------------------------------------------------    
  24. Loan.java
  25. ------------------------------------------------------------------------------------------------------------------------------------
  26. public class Loan implements Serializable, ILoan {
  27.  
  28.     private int loanId;
  29.     private IBook book;
  30.     private IPatron patron;
  31.     private Date dueDate;
  32.     private LoanState state;
  33.  
  34.    
  35.     public Loan(IBook book, IPatron patron) {
  36.         this.book = book;
  37.         this.patron = patron;
  38.  
  39.         this.state = LoanState.PENDING;
  40.     }
  41.  
  42.     @Override
  43.     public void commit(int loanId, Date dueDate) {
  44.         if (state != LoanState.PENDING) {
  45.             throw new RuntimeException("Cannot commit a non PENDING loan");
  46.         }
  47.         this.loanId = loanId;
  48.         this.dueDate = dueDate;
  49.         this.state = LoanState.CURRENT;
  50.        
  51.         patron.takeOutLoan(this);
  52.         book.borrowFromLibrary();                
  53.     }
  54.  
  55. ---------------------------------------------------------------------------------------------------------------------
  56. ILoan.java
  57. ---------------------------------------------------------------------------------------------------------------------
  58. package library.entities;
  59.  
  60. import java.util.Date;
  61.  
  62. public interface ILoan {
  63.  
  64.     enum LoanState {
  65.         PENDING, CURRENT, OVER_DUE, DISCHARGED
  66.     };
  67.  
  68.     int getId();
  69.    
  70.     boolean checkOverDue(Date currentDate);
  71.  
  72.     boolean isOverDue();
  73.  
  74.     Date getDueDate();
  75.  
  76.     IPatron getPatron();
  77.  
  78.     IBook getBook();
  79.    
  80.     void commit(int loanId, Date dueDate);
  81.  
  82.     void discharge(boolean isDamaged);
  83.  
  84. }
RAW Paste Data