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;
      }
    
    }
    
    
  • 相关阅读:
    分梨
    18岁生日
    马的移动
    摆积木
    杭电2093考试排名
    栈的应用——四则运算表达式求值
    用指针实现对二维数组元素的访问
    用多种方法访问字符数组元素
    fread()函数和fwrite()函数进行文件操作
    hdu-1431 素数回文
  • 原文地址:https://www.cnblogs.com/jieyuefeng/p/12300415.html
Copyright © 2011-2022 走看看