first of all I like using stackoverflow and so far i was able to find a soulution for most of my problems. Now I'm kinda stuck with something I couldn't find anything about and hope to find some advice.
I'm working on my UnitTests related to a project that uses WebDrivers, WebElements and so on. I got pretty far with mocking connections and everything but there is one problem I'm stuck with.
I got a try block in my class that looks similar to
class MyClass {
myMethod() {
(...)
try {
WebElement element = webDriver.findElement(By.id(elementId));
element.click();
} catch (NoSuchElementException e) {
throw new MyException("Unable to locate element!", e);
}
(...)
}
}
Now i'm trying to write a testcase where that element was not found
class MyTestClass {
testMyMethod() {
WebElement mockedElement = Mockito.mock(WebElement.class);
Mockito.when(mockedWebDriver.findElement(By.id(Mockito.anyString()))).thenReturn(mockedElement);
Mockito.doThrow(NoSuchElementException.class).when(mockedAllowElement).click();
try {
mockedObjectOfMyClass.myMethod()
Assert.fail();
} catch (MyException e) {
Assert.assertTrue(e.getCause() instanceof NoSuchElementException);
}
}
}
My problem is that i get the lovely NoSuchElementException instead of the expected MyException:
java.util.NoSuchElementException
Process finished with exit code -1
I checked everything i could think of. During the test hashCodes of all mocked objects in the test method are identical to the ones in the real methods and myMethod runs up to the "element.click()". But the exception is not caught by the catch block.
I must be missing something, I'm quite new to Mockito and UnitTesting in general.
Hopefully I didn't leave out anything important while shortening my logic :).
I'll appreciate any ideas or hints
Aucun commentaire:
Enregistrer un commentaire