8000 #111 Begun replacing mockito with hand-built test doubles · seleniumQuery/seleniumQuery@ca96181 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca96181

Browse files
committed
#111 Begun replacing mockito with hand-built test doubles
1 parent b121185 commit ca96181

File tree

8 files changed

+242
-32
lines changed

8 files changed

+242
-32
lines changed

src/test/java/io/github/seleniumquery/functions/jquery/events/ClickFunctionTest.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015 seleniumQuery authors
2+
* Copyright (c) 2016 seleniumQuery authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,13 +22,15 @@
2222
import org.junit.Test;
2323
import org.openqa.selenium.ElementNotVisibleException;
2424
import org.openqa.selenium.WebElement;
25+
import testinfrastructure.testdouble.org.openqa.selenium.WebElementClickSpy;
2526
import testinfrastructure.testutils.LogInjector;
2627

2728
import static org.hamcrest.core.Is.is;
2829
import static org.junit.Assert.assertThat;
2930
import static org.mockito.Mockito.*;
30-
import static testinfrastructure.testdouble.Mocks.createWebElementMock;
3131
import static testinfrastructure.testdouble.SeleniumQueryObjectMother.createStubSeleniumQueryObjectWithElements;
32+
import static testinfrastructure.testdouble.org.openqa.selenium.WebElementMother.createClickableWebElement;
33+
import static testinfrastructure.testdouble.org.openqa.selenium.WebElementMother.createUnclickableHiddenWebElement;
3234

3335
public class ClickFunctionTest {
3436

@@ -45,33 +47,33 @@ public void click__shouldReturnPassedSeleniumQueryObject() {
4547
@Test
4648
public void click__shouldCallClickOnEveryElementIfNoneThrowsException() {
4749
// given
48-
WebElement webElementClickSpyOne = createWebElementClickSpy();
49-
WebElement webElementClickSpyTwo = createWebElementClickSpy();
50+
WebElementClickSpy webElementClickSpyOne = new WebElementClickSpy();
51+
WebElementClickSpy webElementClickSpyTwo = new WebElementClickSpy();
5052
SeleniumQueryObject sqo = createStubSeleniumQueryObjectWithElements(webElementClickSpyOne, webElementClickSpyTwo);
5153
// when
5254
ClickFunction.click(sqo);
5355
// then
54-
assertNumberOfTimesClicked(webElementClickSpyOne, 1);
55-
assertNumberOfTimesClicked(webElementClickSpyTwo, 1);
56+
webElementClickSpyOne.assertNumberOfTimesClicked(1);
57+
webElementClickSpyTwo.assertNumberOfTimesClicked(1);
5658
}
5759

5860
@Test
5961
public void click__shouldCallClickOnEveryElementEvenIfSomeThrowExceptions() {
6062
// given
6163
WebElement unclickableHiddenWebElement = createUnclickableHiddenWebElement();
62-
WebElement webElementClickSpy = createWebElementClickSpy();
64+
WebElementClickSpy webElementClickSpy = new WebElementClickSpy();
6365
SeleniumQueryObject sqo = createStubSeleniumQueryObjectWithElements(unclickableHiddenWebElement, webElementClickSpy);
6466
// when
6567
ClickFunction.click(sqo);
6668
// then
67-
assertNumberOfTimesClicked(webElementClickSpy, 1);
69+
webElementClickSpy.assertNumberOfTimesClicked(1);
6870
}
6971

7072
@Test
7173
public void click__shouldLogInfoIfAnyElementThrewException() {
7274
// given
7375
Log logSpy = LogInjector.injectLogSpy(ClickFunction.class);
74-
SeleniumQueryObject sqo = createStubSeleniumQueryObjectWithElements(createUnclickableHiddenWebElement(), createWebElementMock());
76+
SeleniumQueryObject sqo = createStubSeleniumQueryObjectWithElements(createUnclickableHiddenWebElement(), createClickableWebElement());
7577
// when
7678
ClickFunction.click(sqo);
7779
// then
@@ -90,18 +92,4 @@ public void click__shouldThrowExceptionIfNoElementIsClickable() {
9092
// exception is thrown
9193
}
9294

93-
private void assertNumberOfTimesClicked(WebElement webElementClickSpy, int numberOfTimesClicked) {
94-
verify(webElementClickSpy, times(numberOfTimesClicked)).click();
95-
}
96-
97-
private WebElement createWebElementClickSpy() {
98-
return createWebElementMock();
99-
}
100-
101-
private WebElement createUnclickableHiddenWebElement() {
102-
WebElement unclickableHiddenWebElement = createWebElementMock();
103-
doThrow(new ElementNotVisibleException("This web element is hidden, thus not clickable.")).when(unclickableHiddenWebElement).click();
104-
return unclickableHiddenWebElement;
105-
}
106-
10795
}

src/test/java/testinfrastructure/testdouble/Mocks.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015 seleniumQuery authors
2+
* Copyright (c) 2016 seleniumQuery authors
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,18 +17,12 @@
1717
package testinfrastructure.testdouble;
1818

1919
import org.openqa.selenium.WebDriver;
20-
import org.openqa.selenium.WebElement;
21-
22-
import static org.mockito.Mockito.mock;
20+
import testinfrastructure.testdouble.org.openqa.selenium.WebDriverDummy;
2321

2422
public class Mocks {
2523

26-
public static WebElement createWebElementMock() {
27-
return mock(WebElement.class);
28-
}
29-
3024
public static WebDriver mockWebDriver() {
31-
return mock(WebDriver.class);
25+
return new WebDriverDummy();
3226
}
3327

3428
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2016 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package testinfrastructure.testdouble;
18+
19+
public class PseudoTestDoubleException extends RuntimeException {
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2016 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package testinfrastructure.testdouble.org.openqa.selenium;
18+
19+
import org.openqa.selenium.By;
20+
import org.openqa.selenium.WebDriver;
21+
import org.openqa.selenium.WebElement;
22+
import testinfrastructure.testdouble.PseudoTestDoubleException;
23+
24+
import java.util.List;
25+
import java.util.Set;
26+
27+
public class WebDriverDummy implements WebDriver {
28+
29+
@Override public void get(String s) { throw new PseudoTestDoubleException(); }
30+
@Override public String getCurrentUrl() { throw new PseudoTestDoubleException(); }
31+
@Override public String getTitle() { throw new PseudoTestDoubleException(); }
32+
@Override public List<WebElement> findElements(By by) { throw new PseudoTestDoubleException(); }
33+
@Override public WebElement findElement(By by) { throw new PseudoTestDoubleException(); }
34+
@Override public String getPageSource() { throw new PseudoTestDoubleException(); }
35+
@Override public void close() { throw new PseudoTestDoubleException(); }
36+
@Override public void quit() { throw new PseudoTestDoubleException(); }
37+
@Override public Set<String> getWindowHandles() { throw new PseudoTestDoubleException(); }
38+
@Override public String getWindowHandle() { throw new PseudoTestDoubleException(); }
39+
@Override public WebDriver.TargetLocator switchTo() { throw new PseudoTestDoubleException(); }
40+
@Override public WebDriver.Navigation navigate() { throw new PseudoTestDoubleException(); }
41+
@Override public WebDriver.Options manage() { throw new PseudoTestDoubleException(); }
42+
43+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2016 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package testinfrastructure.testdouble.org.openqa.selenium;
18+
19+
import static org.hamcrest.core.Is.is;
20+
import static org.junit.Assert.assertThat;
21+
22+
public class WebElementClickSpy extends WebElementDummy {
23+
24+
private int clickCount = 0;
25+
26+
@Override
27+
public void click() {
28+
clickCount++;
29+
}
30+
31+
public void assertNumberOfTimesClicked(int numberOfTimesClicked) {
32+
assertThat(clickCount, is(numberOfTimesClicked));
33+
}
34+
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2016 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package testinfrastructure.testdouble.org.openqa.selenium;
18+
19+
import org.openqa.selenium.*;
20+
import testinfrastructure.testdouble.PseudoTestDoubleException;
21+
22+
import java.util.List;
23+
24+
public class WebElementDummy implements WebElement {
25+
26+
@Override public void click() { throw new PseudoTestDoubleException(); }
27+
@Override public void submit() { throw new PseudoTestDoubleException(); }
28+
@Override public void sendKeys(CharSequence... charSequences) { throw new PseudoTestDoubleException(); }
29+
@Override public void clear() { throw new PseudoTestDoubleException(); }
30+
@Override public String getTagName() { throw new PseudoTestDoubleException(); }
31+
@Override public String getAttribute(String s) { throw new PseudoTestDoubleException(); }
32+
@Override public boolean isSelected() { throw new PseudoTestDoubleException(); }
33+
@Override public boolean isEnabled() { throw new PseudoTestDoubleException(); }
34+
@Override public String getText() { throw new PseudoTestDoubleException(); }
35+
@Override public List<WebElement> findElements(By by) { throw new PseudoTestDoubleException(); }
36+
@Override public WebElement findElement(By by) { throw new PseudoTestDoubleException(); }
37+
@Override public boolean isDisplayed() { throw new PseudoTestDoubleException(); }
38+
@Override public Point getLocation() { throw new PseudoTestDoubleException(); }
39+
@Override public Dimension getSize() { throw new PseudoTestDoubleException(); }
40+
@Override public Rectangle getRect() { throw new PseudoTestDoubleException(); }
41+
@Override public String getCssValue(String s) { throw new PseudoTestDoubleException(); }
42+
@Override public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException { throw new PseudoTestDoubleException(); }
43+
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2016 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package testinfrastructure.testdouble.org.openqa.selenium;
18+
19+
import org.openqa.selenium.ElementNotVisibleException;
20+
import org.openqa.selenium.WebElement;
21+
22+
public class WebElementMother {
23+
24+
public static WebElement createUnclickableHiddenWebElement() {
25+
return new WebElementStub() {
26+
// this has to be a STUB and not a DUMMY because when the $().click() function fails, it
27+
// call a series of other functions on WebElement just to print a more complete error message
28+
@Override
29+
public void click() {
30+
throw new ElementNotVisibleException("This web element is hidden, thus not clickable.");
31+
}
32+
};
33+
}
34+
35+
public static WebElement createClickableWebElement() {
36+
return new WebElementDummy() {
37+
@Override
38+
public void click() { }
39+
};
40+
}
41+
42+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2016 seleniumQuery authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package testinfrastructure.testdouble.org.openqa.selenium;
18+
19+
import org.openqa.selenium.*;
20+
import testinfrastructure.testdouble.PseudoTestDoubleException;
21+
22+
import java.util.List;
23+
24+
public class WebElementStub implements WebElement {
25+
26+
@Override public void click() { }
27+
@Override public void submit() { }
28+
@Override public void sendKeys(CharSequence... charSequences) { }
29+
@Override public void clear() { }
30+
@Override public String getTagName() { return null; }
31+
@Override public String getAttribute(String s) { return null; }
32+
@Override public boolean isSelected() { return false; }
33+
@Override public boolean isEnabled() { return false; }
34+
@Override public String getText() { return null; }
35+
@Override public List<WebElement> findElements(By by) { return null; }
36+
@Override public WebElement findElement(By by) { return null; }
37+
@Override public boolean isDisplayed() { return false; }
38+
@Override public Point getLocation() { return null; }
39+
@Override public Dimension getSize() { return null; }
40+
@Override public Rectangle getRect() { return null; }
41+
@Override public String getCssValue(String s) { return null; }
42+
@Override public <X> X getScreenshotAs(OutputType<X> outputType) throws WebDriverException { return null; }
43+
44+
}

0 commit comments

Comments
 (0)
0