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;
}
}