'Mock page factory and appium with mockito

I am (beginner, so go easy on me), trying to write unit tests for some of the custom functions I wrote on an appium test framework.

public class BasePageNewTest {

private BasePage basePage;
private AppiumDriver appiumDriver;


@BeforeEach
void setup() throws MalformedURLException {
    appiumDriver = mock(AppiumDriver.class,withSettings()
                  .useConstructor(
                   Mockito.any(URL.class),
                   Mockito.any(DesiredCapabilities.class)
                   ));

    basePage = new BasePage(appiumDriver);
    //basePage = mock(BasePage.class,withSettings().useConstructor(appiumDriver));
}

@Test
//Dummy function to test the mock
void checkAdd() {
    int sum = 5;
    //when(basePage.add(Mockito.any(), Mockito.any())).thenReturn(sum);
    assertEquals(5,basePage.add(3, 2));
}

BasePage:

public class BasePage {
    public AppiumDriver<?> driver;

    public BasePage(AppiumDriver driver) {
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }

    public int add(int a, int b) {
        return a + b;
      }
}

Error:

org.mockito.exceptions.base.MockitoException: Unable to create mock instance of type 'AppiumDriver'
  1. How can we mock appium Driver and page factory with mockito?
  2. if it is not possible to mock, why?
  3. Testing the custom methods can only be possible thru integration testing with AppiumDriverLocalService or cloud device farms?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source