zoukankan      html  css  js  c++  java
  • 单元测试DEMO

    import lombok.extern.slf4j.Slf4j;
    import org.junit.Before;
    import org.junit.Test;
    import org.mockito.Mockito;
    
    import java.lang.reflect.Field;
    import java.util.Optional;
    
    @Slf4j
    public class AppServiceImplTest {
      private AppServiceImpl appService = new AppServiceImpl();
    
      private AppDao appDao;
    
      @Before
      public void initAppService() {
        try {
          appDao = Mockito.mock(AppDao.class);
          Field field = appService.getClass().getDeclaredField("appDao");
          field.setAccessible(true);
          field.set(appService, appDao);
        } catch (NoSuchFieldException | IllegalAccessException e) {
          log.error("initAppService failed", e);
        }
      }
    
      @Test
      public void testFindById_Normal() {
        Mockito.doReturn(Optional.of(new App())).when(appDao).findById(1L);
        assert appService.findById(1L) != null;
      }
    
      @Test
      public void testFindById_Null() {
        Mockito.doReturn(Optional.empty()).when(appDao).findById(null);
        assert appService.findById(null) == null;
      }
    
      @Test
      public void testFindById_NotExists() {
        Mockito.doReturn(Optional.empty()).when(appDao).findById(Long.MAX_VALUE);
        assert appService.findById(Long.MAX_VALUE) == null;
      }
    
    }
    
    
  • 相关阅读:
    最简单的UDP程序
    最简单的TCP程序
    一道面试题的分析
    JDK5新特性:可变参数方法
    文件IO流总结
    集合使用的总结
    双列集合Map的嵌套遍历
    集合HashSet的使用
    集合TreeSet的使用
    用LinkedList模拟Stack功能
  • 原文地址:https://www.cnblogs.com/jieyuefeng/p/12300415.html
Copyright © 2011-2022 走看看