Mockito 测试通过除了一个验证

Mockito tests pass except one verify

除了第一个测试中的这一行,我的所有测试都通过了

验证(reimbursementDAO).getById(REIMBURSEMENT_TO_PROCESS.getId()); 请参阅下面的代码。

package com.revature.services;


public class ReimbursementServiceTest {

    private static ReimbursementService reimbursementService;
    private static ReimbursementDAO reimbursementDAO;
    
    private Reimbursement REIMBURSEMENT_TO_PROCESS;
    private Reimbursement GENERIC_REIMBURSEMENT_1;
    private Optional<Reimbursement> 
      GENERIC_REIMBURSEMENT_2;
    private List<Reimbursement> GENERIC_ALL_PENDING_REIMBURSEMENTS;
    private User GENERIC_EMPLOYEE_1;
    private User GENERIC_FINANCE_MANAGER_1;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        
        reimbursementDAO=mock(ReimbursementDAO.class);//(IReimbursementDAO.class);
        reimbursementService = new ReimbursementService(reimbursementDAO);
        //reimbursementDAO=new ReimbursementDAO();
    }
    
    @Before
    public void setUp() throws Exception {
        GENERIC_EMPLOYEE_1 = new User(1, "genericEmployee1", "genericPassword", Role.EMPLOYEE);
        GENERIC_FINANCE_MANAGER_1 = new User(1, "genericManager1", "genericPassword", Role.FINANCE_MANAGER);
        
        REIMBURSEMENT_TO_PROCESS = new Reimbursement(2, Status.PENDING, GENERIC_EMPLOYEE_1, null, 150.00);
        
        GENERIC_REIMBURSEMENT_1 = new Reimbursement(1, Status.PENDING, GENERIC_EMPLOYEE_1, null, 100.00);
        GENERIC_REIMBURSEMENT_2 = Optional.ofNullable(new Reimbursement(2, Status.APPROVED, GENERIC_EMPLOYEE_1,
            GENERIC_FINANCE_MANAGER_1, 150.00));
        
        GENERIC_ALL_PENDING_REIMBURSEMENTS = new ArrayList<Reimbursement>();
        GENERIC_ALL_PENDING_REIMBURSEMENTS.add(GENERIC_REIMBURSEMENT_1);
    }
    
    @Test
    public void testProcessPassesWhenUserIsFinanceManagerAndReimbursementExistsAndUpdateSuccessful()
    throws Exception{
     
        when(reimbursementDAO.getById(anyInt())).thenReturn(Optional.of(GENERIC_REIMBURSEMENT_1));
        when(reimbursementDAO.update(any())).thenReturn(GENERIC_REIMBURSEMENT_2);
        
        assertEquals(GENERIC_REIMBURSEMENT_2,
                reimbursementService.process(REIMBURSEMENT_TO_PROCESS, Status.APPROVED, 
                        
                        GENERIC_FINANCE_MANAGER_1));
        
        //verify(reimbursementDAO).getById(REIMBURSEMENT_TO_PROCESS.getId());
        verify(reimbursementDAO).update(REIMBURSEMENT_TO_PROCESS);
        
    }
    
    @Test
    public void testGetReimbursementByStatusPassesWhenReimbursementsAreSuccessfullyReturned() {
        when(reimbursementDAO.getBystatus(any())).thenReturn(GENERIC_ALL_PENDING_REIMBURSEMENTS);
        
        assertEquals(GENERIC_ALL_PENDING_REIMBURSEMENTS, 
                reimbursementService.getReimbursementsByStatus(Status.PENDING));
        
        verify(reimbursementDAO).getBystatus(Status.PENDING);
    }
}
public class ReimbursementDAO  extends AbstractReimbursement 
{

        public Optional< Reimbursement> getById(int id) {
        
        try(Connection conn = ConnectionFactory.getConnection())
        {
            String sql="select * from ers_reimbursements where reimb_id=?;";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
        ResultSet rs=   ps.executeQuery();
            Reimbursement reimb=null;
            UserService usrv=new UserService();
            //reimb_id ,amount, submitted,resolved,description,author,receipt ,resolver,status,reimb_type
            while(rs.next())
            {
                
                int reid=rs.getInt("reimb_id");
                double ramount=rs.getInt("reimb_amount");
                int res=rs.getInt(  "resolver");
                 User resolver=null;
                
                String description=rs.getString("description");
                User rauthor= usrv.getUserById(  rs.getInt("author")).get();
                if(res>0)
                { resolver=  usrv.getUserById(res).get(); }
                 int rstatus=   rs.getInt("reimb_status");
                 Status r_status=Status.values()[--rstatus];             
                 int reimb_type=   rs.getInt("reimb_type");
                 ReimbType retype=ReimbType.values()[--reimb_type];
                
                User oth=rauthor;
                User re=resolver;
                 reimb=new Reimbursement(reid, r_status,oth,re,ramount);
                 return Optional.ofNullable(reimb);
            }
            
            
        }catch(SQLException  e) { e.printStackTrace();};
        
        return Optional.empty();
    }

   
    public  List<Reimbursement> getBystatus(Status status) {
        
        try(Connection conn = ConnectionFactory.getConnection())
        {
            String sql="select * from ers_reimbursements where reimb_status=?;";
            PreparedStatement ps=conn.prepareStatement(sql);//,Statement.RETURN_GENERATED_KEYS);
            int sta_id= status.ordinal()+1;
        
            ps.setInt(1,sta_id);
        ResultSet rs=   ps.executeQuery();
            Reimbursement reimb=null;
            List<Reimbursement> reimbList=new ArrayList<Reimbursement>();
            IUserService usrv=new UserService();
            //reimb_id ,amount, submitted,resolved,description,author,receipt ,resolver,status,reimb_type
            while(rs.next())
            {
                //int id, Status status, User author, User resolver, double amount
                int reid=rs.getInt("reimb_id");
                double ramount=rs.getInt("reimb_amount");
                
                
            Optional<User> rauthor=  usrv.getUserById(  rs.getInt("author"));
            User  oth=null;
            if(rauthor.isPresent())
             {     oth=rauthor.get();   }
                int resol=rs.getInt(  "resolver");
                 Optional<User> resolver=  usrv.getUserById(resol);
                 User re=null;
                
                  if(resolver.isPresent())
                 {     re=resolver.get();    }
                 else {re=null;}
                 int rstatus=   rs.getInt("reimb_status");
                 Status r_status=Status.values()[--rstatus];//.PENDING;
                 
                 
                 int reimb_type=   rs.getInt("reimb_type");
                 ReimbType retype=ReimbType.values()[--reimb_type];//.TRAVEL;
                
                
                 reimb=new Reimbursement(reid, r_status,oth,re,ramount);
                 reimbList.add(reimb);
            }
            
            return   reimbList;
        }catch(SQLException  e) { e.printStackTrace();};
        
        return null;
        
        
        
        
    }

   
    public Optional<Reimbursement> update(Reimbursement unprocessedReimbursement) {
        
        try(Connection conn=ConnectionFactory.getConnection()) {
            
            String sql="update  ers_reimbursements set reimb_status=?,"
                    + " resolver=?, resolved=?  where reimb_id=?;";
            
            PreparedStatement ps=conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            int id=unprocessedReimbursement.getId();
            Status st=unprocessedReimbursement.getStatus();
            ps.setObject(1,st);                 
            
                ps.setInt(2,unprocessedReimbursement.getResolver().getId());
            ps.setObject(3, LocalDateTime.now());
            ps.setInt(4,id);
            ps.executeUpdate();
            
            try (ResultSet generatedKeys = ps.getGeneratedKeys()) {
                if (generatedKeys.next()) {
                    int reimid=generatedKeys.getInt(1);
                   Optional<Reimbursement> reim=getById(reimid);
                    System.out.println("Reimb " + reim.get()+ " upLocalTimed!");
                    return reim;
                }
            }catch(SQLException e) {};
            
            
            
        }catch(SQLException e) { e.printStackTrace();}
        return Optional.empty();
    }   
}
public class ReimbursementService{
 {
    
    
    private final ReimbursementDAO reimbDao;

      public ReimbursementService() {
        this(new ReimbursementDAO());
      }

      
      public ReimbursementService(ReimbursementDAO userDAO2) {
        this.reimbDao = userDAO2;
        
      }
    
    

        public Optional<  Reimbursement> process(Reimbursement unprocessedReimbursement,
            Status finalStatus, User resolver) throws Exception{
            
       
           if (!resolver.getRole().equals(Role.FINANCE_MANAGER)) {
               throw new RegistrationUnsuccessfulException("Resolver must be Finance Manager ");
            
        }
        // List<Reimbursement> l=DAO.getByStatus(Status.PENDING);
           if(unprocessedReimbursement.getId()==0)
           {     throw new Exception(" reimbursement not found"); }
               
           if(unprocessedReimbursement.getStatus().equals(Status.PENDING))
           
           {
               unprocessedReimbursement.setResolver(resolver);
               unprocessedReimbursement.setStatus(finalStatus);
               Optional<Reimbursement> reimb=this.reimbDao.update(unprocessedReimbursement );
               if(reimb.isPresent())  
               { return reimb; }
               else { throw new Exception("unsuccessful update");}
           }
           
            return Optional.ofNullable(null);
           
        
        
    }

    
}

验证

verify(reimbursementDAO).getById(REIMBURSEMENT_TO_PROCESS.getId());

失败,因为您的服务没有调用 DAO 的 getById() 方法。

碰巧你真正的 DAO 的 update() 方法调用它自己的 getById() 方法,但在你的测试中你使用的是模拟 DAO,其中所有功能都被删除了。模拟 DAO 的 update() 方法只做 return GENERIC_REIMBURSEMENT_2 因为那是你的测试设置它要做的。